7

This kills every process with a handle to file /foo/bar (in bash):

 lsof /foo/bar 2>&1 | grep "/foo/bar" | sed "s/  */\\t/g" | cut -f 2 | while read PID; do kill $PID; done

This does not seem like such an uncommon task that there wouldn't be an easier solution, so I'm wondering if there's something like killall or a switch to kill that I've missed which does the same.

1 Answer 1

10

That's what -t is for. The man page even suggests you'd use that for kill.

lsof -t /some/file | xargs kill

Traditionally (before the lsof days), you'd use:

fuser /some/file 2> /dev/null | xargs kill

for that.

Some fuser implementations, like the one found on most Linux-based operating systems , Solaris or recent FreeBSDs can even do the killing by themselves:

fuser -k /some/file

However note that they send a SIGKILL, not SIGTERM. You can choose a different signal with -TERM with some implementations and -s TERM with others.

1
  • Nice - also, I didn't know about xargs. Commented Feb 12, 2014 at 9:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .