Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I kill all processes running from a particular directory?

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.

like image 911
Timothy Armstrong Avatar asked Dec 30 '25 01:12

Timothy Armstrong


2 Answers

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 ids
  • while read line; do pwdx $line 2> /dev/null; done: gets more info about each process, including the directory it's running from
  • 2> /dev/null: removes error lines about processes you don't have permissions to (optional)
  • cut -d':' -f1 extracts only the pids
  • while read line; do kill $line; done;: kills each process by pid
like image 173
electrovir Avatar answered Jan 01 '26 17:01

electrovir


I use this command to kill process in a specific directory:

lsof | grep '\/path\/to\/processes\/' | awk '{print $2}' | xargs kill
like image 39
MikeRogers Avatar answered Jan 01 '26 16:01

MikeRogers