Monitoring Processes with Kill

August 14th, 2008 by LJ Staff in

Your rating: None Average: 4.2 (6 votes)

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

__________________________


Special Magazine Offer -- Free Gift with Subscription
Receive a free digital copy of Linux Journal's System Administration Special Edition as well as instant online access to current and past issues. CLICK HERE for offer

Linux Journal: delivering readers the advice and inspiration they need to get the most out of their Linux systems since 1994.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Marcelo Magallon's picture

Not existance but possibility to send signal

On August 24th, 2008 Marcelo Magallon (not verified) says:

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?"

sg's picture

/proc/pid

On August 16th, 2008 sg (not verified) says:

nice trick but checking if /proc/ exists is a more straightforward approach in my opinion

Anonymous's picture

Trying to clarify missing parts

On August 15th, 2008 Anonymous (not verified) says:

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!

Anonymous's picture

Please fix this posting

On August 15th, 2008 Anonymous (not verified) says:

Can someone please fix this entry and make the code show up? TIA.

Webmistress's picture

Fixed!

On August 15th, 2008 Webmistress says:

Sorry about that!

__________________________

Katherine Druckman is webmistress at LinuxJournal.com. You might find her chatting on the IRC channel.

Anonymous's picture

No problem

On August 16th, 2008 Anonymous (not verified) says:

... but there still is some XML stuff showing up. HTH.

You're such a tease.

Anonymous's picture

Well said! I like it, too:)

On September 5th, 2008 Anonymous (not verified) says:

Well said! I like it, too:)

goblin's picture

Think "The Matrix"

On August 15th, 2008 goblin says:

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!

Post new comment

Please note that comments may not appear immediately, so there is no need to repost your comment.
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd> <i> <b>
  • Lines and paragraphs break automatically.

More information about formatting options

Newsletter

Each week Linux Journal editors will tell you what's hot in the world of Linux. You will receive late breaking news, technical tips and tricks, and links to in-depth stories featured on www.linuxjournal.com.
Sign up for our Email Newsletter

Tech Tip Videos

From the Magazine

July 2009, #183

News Flash: Linux Kernel 3.0 to include an on-the-go Expresso machine interface! Ok, maybe not, but Linux is definitely going mobile, from phones to e-readers. Find out more inside about Android, the Kindle 2, the Western Digital MyBook II, The Bug, and Indamixx (a portable recording studio). And if you've gone mobile and you been wanting more Emacs in your life then check out Conkeror.


To compliment the mobile we've got the stationary: parsing command line options with getopt, checking your Ruby code with metric_fu, and building a secure Squid proxy. How is this stationary you ask? What can we say? It's not. We just wanted to see if anybody actually read this part of the page :) .


All this and more, and all you have to do is get your hot sweaty hands on the latest copy of Linux Journal.





Read this issue