1

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?

Victor Mezrin
  • 113
  • 1
  • 7

3 Answers3

3

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)
krisFR
  • 13,280
  • 4
  • 36
  • 42
1

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>
0

Assuming you're referring to Windows you can use Process Monitor from the Windows SysInternals suite

DKNUCKLES
  • 4,028
  • 9
  • 47
  • 60