Wednesday, December 30, 2009

Check if a process with pid exists

Today I came across a problem where I have to check if a process with a pid exists.

The pid I wanted to check is in a file xxx.pid. The following is what I did


kill -0 `cat xxx.pid`
if [ $? -eq 0 ]
then
echo "XXX is running"
else
echo "XXX is not running"
fi


From Linux kill man page:
If sig is 0, then no signal is sent, but error checking is still performed.

From Solaris kill(2) man page
If sig is 0 (the null signal), error checking is performed but no signal is actually sent. This can be used to check the validity of pid.

Thanks to the following post

http://fahdshariff.blogspot.com/2008/08/check-if-process-is-running-unix.html

No comments:

Post a Comment