Reputation: 59
I have multiple python sessions under certain directory like,
lsof test11/
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
python 5838 user1 cwd DIR 8,34 4096 19947539 test11
python 5840 user1 cwd DIR 8,34 4096 19947539 test11
python 5843 user1 cwd DIR 8,34 4096 19947539 test11
python 5845 user1 cwd DIR 8,34 4096 19947539 test11
python 5846 user1 cwd DIR 8,34 4096 19947539 test11
python 5847 user1 cwd DIR 8,34 4096 19947539 test11
bash 68363 user1 cwd DIR 8,34 4096 19947539 test11
python 68510 user1 cwd DIR 8,34 4096 19947539 test11
Can can I kill above all python sessions (except bash) in a simple batch script? Thanks.
Upvotes: 0
Views: 1032
Reputation: 4963
Combine kill
with lsof
:
kill $(lsof -c python -ta +D /test11/)
A kill
signal is sent to the terse output of lsof
, (which is a list of python
pids), active in the /test11/
directory.
Upvotes: 0
Reputation: 108
As mentioned in https://unix.stackexchange.com/a/50573/191874. Below will do the needful.
for pid in $(lsof test11/ | grep "python" | awk '{print $2}'); do kill -9 $pid; done
Upvotes: 1