Loading
Home ›
Monitoring Processes with Kill
Sep 23, 2008 By Rich Lundeen
in
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
______________________
Trending Topics
| You Need A Budget | Feb 10, 2012 |
| The Linux powered LAN Gaming House | Feb 08, 2012 |
| Creating a vDSO: the Colonel's Other Chicken | Feb 06, 2012 |
| Your CMS Is Not Your Web Site | Feb 01, 2012 |
| Casper, the Friendly (and Persistent) Ghost | Jan 31, 2012 |
| Razor-qt 0.4 - Qt based Desktop Environment | Jan 30, 2012 |
- Fun with ethtool
- Parallel Programming with NVIDIA CUDA
- Readers' Choice Awards 2011
- 100% disappointed with the decision to go all digital.
- Linux-Based X Terminals with XDMCP
- Validate an E-Mail Address with PHP, the Right Way
- You Need A Budget
- The Linux powered LAN Gaming House
- Why Python?
- Python for Android
- Employment Posters
3 hours 3 min ago - Sure the best distro is
4 hours 23 min ago - BeOS was the best
7 hours 7 min ago - I use Wireshark on a daily
11 hours 37 min ago - buena información
16 hours 44 min ago - One important "bucket" that I didn't note (désolé si qqun deja d
17 hours 44 min ago - Gnome3 is such a POS. No one
1 day 3 hours ago - Gnome 3 is the biggest POS
1 day 3 hours ago - I didn't knew this thing by
1 day 9 hours ago - Author's reply
1 day 12 hours ago





Comments
ps "parsing"
I think you should parse the ps output because in theory it is possible that your process died and (another) new process got its PID. It just depends on how often you check if the PID is still running and how often new processes get spawned on your system.
But by giving ps the right parameters it does all the work for you:
$ ps hp $(cat /var/run/crond.pid) o comm
cron
This way you can confirm that the given process name is indeed the program you are monitoring.