Monitoring Processes with Kill
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
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- RSS Feeds
- Trying to Tame the Tablet
- New Products
- What's the tweeting protocol?
- Dart: a New Web Programming Experience
- Reply to comment | Linux Journal
2 hours 25 min ago - Drupal is an Awesome CMS and a Crappy development framework
7 hours 4 min ago - IT industry leaders
9 hours 27 min ago - Reply to comment | Linux Journal
1 day 2 hours ago - Reply to comment | Linux Journal
1 day 4 hours ago - Reply to comment | Linux Journal
1 day 6 hours ago - great post
1 day 6 hours ago - Google Docs
1 day 7 hours ago - Reply to comment | Linux Journal
1 day 11 hours ago - Reply to comment | Linux Journal
1 day 12 hours ago
Enter to Win an Adafruit Prototyping Pi Plate Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Prototyping Pi Plate Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.



Comments
Not existance but possibility to send signal
Nice idea.
Just to make things clear, as the documentation states, when sending signal 0 "exit code indicates if a signal may be sent".
Try this as a regular user:
$ kill -0 1
I think we all agree on the fact that there *is* a process with id 1, we even know which process it is. If you call that command as a regular user, the return value would be non-zero. Following the advise in this article, that would mean that the process isn't running anymore, and we know that that's not true.
Signal 0 not only tests for existance, it also tests for permissions, so to speak. "Am I allowed to send a singal to a given PID?"
/proc/pid
nice trick but checking if /proc/ exists is a more straightforward approach in my opinion
Trying to clarify missing parts
Just to clarify the missing parts.
Suppose you want to know whether a process with ID 1234 is alive. What you might do is to execute 'kill -0 1234' and see the return value of the command. If it's 0, then that process do exist in the system; otherwise, the ID is not registered!
Please fix this posting
Can someone please fix this entry and make the code show up? TIA.
Fixed!
Sorry about that!
Katherine Druckman is webmistress at LinuxJournal.com. You might find her on Twitter or at the Southwest Drupal Summit
No problem
... but there still is some XML stuff showing up. HTH.
For example, to test whether cron is running, do the following:"
You're such a tease.
Well said! I like it, too:)
Well said! I like it, too:)
Think "The Matrix"
Man, don't you see you gotta think like in "The Matrix"? Or "Kung Fu Panda", or whatever...
Just tell yourself "There is no spoon!", over and over again, and the answer will come to you!