5

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.

6

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.

  • Nice - also, I didn't know about xargs. – Johannes Bauer Feb 12 '14 at 9:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.