BASH - Check if PID Exists

JohnP picture JohnP · Mar 5, 2011 · Viewed 51.2k times · Source

I want to stall the execution of my BASH script until a process is closed (I have the PID stored in a variable). I'm thinking

while [PID IS RUNNING]; do
sleep 500
done

Most of the examples I have seen use /dev/null which seems to require root. Is there a way to do this without requiring root?

Thank you very much in advance!

Answer

moonshadow picture moonshadow · Mar 5, 2011

kill -s 0 $pid will return success if $pid is running, failure otherwise, without actually sending a signal to the process, so you can use that in your if statement directly.

wait $pid will wait on that process, replacing your whole loop.