Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to write a robust bash script, and in it I create a background process. At the end of the script, I want to kill it. I have it's PID.

I was thinking of somthing like this

while [[ ps ef $PID ]] ; do
  kill $PID
  sleep 0.5

Any suggests for anything better? Any possible problems with this approach?

share|improve this question
Didn't you mean ps -ef? – grawity Jul 23 '09 at 15:46
grawity: Thats just BSD style vs UNIX options, but ef does seem to fit better with the UNIX options. I would go with 'ps ao pid' myself – Kyle Brandt Jul 23 '09 at 15:59
Watch out for processes in an uninterruptible sleep (top / ps show 'D' for the process status / state). They can't be killed (even by SIGTERM) until whatever I/O they're blocked on completes. Which in the case of a sufficiently horked hard mounted NFS volume could be never. – Mark Johnson Jul 23 '09 at 17:44
up vote 7 down vote accepted

The problem with repeatedly killing a process is that you've got a race condition with new process creation. It's not particularly likely, but it's possible that the process will exit and a new process start up with the same PID while you're sleeping.

Why are you having to repeatedly kill the process? If it's because the process will exit, but it may take some time to quit after receiving the signal, you could use wait:

 kill $PID
 wait $PID

In an ideal system, you'd never have to repeat a kill or issue kill -9 $PID. If you do have to, you might want to consider fixing whatever it is that you're running so you don't have to. In the meantime, you will probably not hit the race condition, and you can guard against it by (say) checking the timestamp of /proc/$PID/ just before killing the process. That's bad hackiness, though.

share|improve this answer

For everyone recommending the step from kill $PID to kill -9 $PID, I'd have to remind you of Useless use of kill -9.

No no no. Don't use kill -9.

It doesn't give the process a chance to cleanly:

  1. shut down socket connections

  2. clean up temp files

  3. inform its children that it is going away

  4. reset its terminal characteristics

and so on and so on and so on.

Generally, send 15, and wait a second or two, and if that doesn't work, send 2, and if that doesn't work, send 1. If that doesn't, REMOVE THE BINARY because the program is badly behaved!

Don't use kill -9. Don't bring out the combine harvester just to tidy up the flower pot.

Now, I don't agree with the "remove the binary part", but the progression seems less damaging than just a kill -9.

I also agree with the care about race conditions on the creation of new processes with the same PID mentioned here.

share|improve this answer

First do your normal kill, Sleep x number of seconds, and then kill -9 it if it still around. Also, this does seem like an odd situation you have gotten into, might want to explain to us the larger issue.

share|improve this answer
Agreed. If it doesn't respond and close after one SIGTERM (the signal sent by default by kill) then is probably wont respond to a 2nd, 3rd, 4th, ...., so you next course of action should be SIGKILL (kill -9). Of course you should make sure that the X in "sleep X seconds" is long enough to allow the process to perform a normal clear-your-desk-and-leave operation even if the system is heavily loaded - it might take a fraction of a second normally but tens of seconds when the system is thrashing. – David Spillett Jul 23 '09 at 16:17

Typically you want to try kill $PID first,

then let it sleep for a bit to see if it will exit gracefully.

If not, then execute kill -9 $PID to forcefully get rid of it.

share|improve this answer

why not just

kill -9 $PID

share|improve this answer
Because you want to give processes a chance to clean up and die nicely, which is what the TERM signal does (the default signal). The KILL signal ("-9") doesn't give processes that chance. – ktower Jul 23 '09 at 16:10
Good point, but the original poster did say he just wanted to kill it. – chris Jul 24 '09 at 14:46

Your Answer


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.