Monitoring Processes with Kill

 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

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Not existance but possibility to send signal

Marcelo Magallon's picture

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

sg's picture

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

Trying to clarify missing parts

Anonymous's picture

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

Anonymous's picture

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

Fixed!

Webmistress's picture

Sorry about that!

Katherine Druckman is webmistress at LinuxJournal.com. You might find her on Twitter or at the Southwest Drupal Summit

No problem

Anonymous's picture

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

For example, to test whether cron is running, do the following:"

theillien's picture

You're such a tease.

Well said! I like it, too:)

Anonymous's picture

Well said! I like it, too:)

Think "The Matrix"

goblin's picture

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!

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState