Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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.

share|improve this answer
    
Nice - also, I didn't know about xargs. –  Johannes Bauer Feb 12 at 9:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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