I'm looking for a quick terminal command that will kill all of the processes which are running from a particular directory (or a subdirectory of that directory).
For example, let's say I have the bin1 and bin2 executables running. They live at the following paths:
/path/to/processes/subdir1/bin1
/path/to/processes/subdir2/subsubdir2/bin2
I want to kill both bin1 and bin2 by only specifying /path/to/processes such that the command will find and kill both bin1 and bin2 because of their location.
Old question I know but I came across this looking for an answer and eventually found one. I don't know if this is the "best" way to do this or not but you gotta start somewhere and I've found this to be very reliable:
ps -eo pid | while read line; do pwdx $line 2> /dev/null; done | grep "your/path/here" | cut -d':' -f1 | while read line; do kill $line; done;
ps -eo pid: lists all process idswhile read line; do pwdx $line 2> /dev/null; done: gets more info about each process, including the directory it's running from2> /dev/null: removes error lines about processes you don't have permissions to (optional)cut -d':' -f1 extracts only the pidswhile read line; do kill $line; done;: kills each process by pidI use this command to kill process in a specific directory:
lsof | grep '\/path\/to\/processes\/' | awk '{print $2}' | xargs kill
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With