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.

ps "parsing"

Anonymous's picture

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.

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

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.

Learn More

Sponsored by Storix