Is there a way to kill all processes that are running in/from a certain directory? Is there a way to kill all processes that are running in/from a certain directory? shell shell

Is there a way to kill all processes that are running in/from a certain directory?


you have to use lsof command then the argument will be the directory you want to kill the process

#!/usr/bin/env bash                                                                                                                                 lsof $(pwd) | \                                                               awk -F " " ' { print $2 } ' | \                                           while read process ; do                                                      [[ ${process} == "PID" ]] && continue;          # kill the processes here          # if you assign each process to a variable or an array          # you will not be able to access it after leaving this while loop          # pipes are executed as subshells          echo ${process};                                                      done


The phrase 'running in' a directory might be interpreted in different ways, some of them are:(1) the program was started from this directory,(2) the program uses this directory as working directory, or(3) the program accesses this directory in any way.

Most likely you have problem (3) and the answer was already given: use

$ fuser -k

but beware, it might kill more than expected!

For problem (1) I would search the processes environment

$ grep -l "/home/username/directory" /proc/*/environ | grep -o [0-9]*

and kill the resulting processes.