get PIDs of processes from
I have a path to an executable. Assume I want to delete this file.
I need to find all processes launched using this file and kill them. What is the best way to do it?
Maybe lsof (list open files) could help you there.
To list all process that are using a specific file:
lsof /path/to/your/specific/file
Adding the -t
option will only return PIDs that use the given file.
So, from there, you can kill all process that are using the specific file :
lsof -t /path/to/your/specific/file | while read PID; do kill -9 $PID; done
Or something like :
kill -9 $(lsof -t /path/to/your/specific/file)
Now, assuming you are in the *nix world, you can use fuser
command.
fuser <file_name>
will list you all process pids using the file. To kill processes accessing the file,
fuser -k <file_name>