If you have a process ID but aren’t sure whether it’s valid, you can use the most unlikely of candidates to test it: the kill command. If you don’t see any reference to this on the kill(1) man page, check the info pages. The man/info page states that signal 0 is special and that the exit code from kill tells whether a signal could be sent to the specified process (or processes).
So kill -0 will not terminate the process, and the return status can be used to determine whether a process is running. For example:
$ echo $$ # show our process id
12833
$ /bin/bash # create new process
$ echo $$ # show new process id
12902
$ kill -0 12902
$ echo $? # exists, exit code is 0
0
$ exit # return to previous shell
$ kill -0 12902
bash: kill: (12902) - No such process
$ echo $? # doesn’t exist, exit code is 1
1
Many UNIX dæmons store their process IDs in a file in /var/run when they are started. Using kill -0 to test the pid is a lot easier than parsing ps output. For example, to test whether cron is running, do the following:
# kill -0 $(cat /var/run/cron.pid)
# echo $?
0
![Digg](http://1.bp.blogspot.com/_lxBSX0YJV58/SwQiO9krTcI/AAAAAAAAAhc/80Z-NLrZ7wU/s1600/digg.png)
![Google Bookmarks](http://1.bp.blogspot.com/_lxBSX0YJV58/SwQitItySOI/AAAAAAAAAhs/RQQh6Du9HrA/s1600/google.png)
![reddit](http://1.bp.blogspot.com/_lxBSX0YJV58/SwQitlmYSuI/AAAAAAAAAh8/mTtVHQxL_mE/s1600/reddit.png)
![Mixx](http://2.bp.blogspot.com/_lxBSX0YJV58/SwQitawG7PI/AAAAAAAAAh0/vF4HOlj8JH8/s1600/mixx.png)
![StumbleUpon](http://2.bp.blogspot.com/_lxBSX0YJV58/SwQit6IUS5I/AAAAAAAAAiE/gfbBSYnbjnU/s1600/stumbleupon.png)
![Technorati](http://4.bp.blogspot.com/_lxBSX0YJV58/SwQiuP6h64I/AAAAAAAAAiM/x_aGzBKYCgc/s1600/technorati.png)
![Yahoo! Buzz](http://1.bp.blogspot.com/_lxBSX0YJV58/SwQi2FtKY5I/AAAAAAAAAiU/ByTK_Wci_Uo/s1600/yahoo.png)
![DesignFloat](http://4.bp.blogspot.com/_lxBSX0YJV58/SwQiOmnjWDI/AAAAAAAAAhU/nMIbaOaCDic/s1600/designfloat.png)
![Delicious](http://4.bp.blogspot.com/_lxBSX0YJV58/SwQiOHLlI8I/AAAAAAAAAhM/6JP39Dh6sF8/s1600/delicious.png)
![BlinkList](http://3.bp.blogspot.com/_lxBSX0YJV58/SwQiN859bNI/AAAAAAAAAhE/E_Uh6XTQmpo/s1600/blinklist.png)
![Furl](http://3.bp.blogspot.com/_lxBSX0YJV58/SwQiPG9Y6xI/AAAAAAAAAhk/YzmjQUPdnrg/s1600/furl.png)