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
Popular Posts
-
du stands for disk usage. This simple linux command provides a summary of harddisk or storage space disk usage. It has many parameter argume...
-
When developing applications, it is common to interact with various tools that require a network connection to function correctly. However, ...
-
HOW TO INSTALL GOOGLE CHROME OS : Do you want to install Google's very own operating system called Chrome OS? If yes, then just follow t...
-
The MBR is a 512 byte segment on the very first sector of your hard drive composed of three parts: 1) the boot code which is 446 bytes long...
-
You can block an IP from IPTABLES by using iptables -I INPUT -s 192.168.0.88 -j DROP You can simplify things a little bit by using a shell s...
-
Linux kernel has in recent times seen numerous advances. And the latest versions of the Linux kernel namely 2.6.28 and 2.6.29 have some fabu...
-
It is good to know that it's possible to play those classicNintendo NES games on any Android device with physical keyboard (Motorola Bac...
-
Ever wanted to access a service behind a firewall that has port 22 open for ssh connections? This is a common setup known as using a jump-bo...
-
This post is contributed by Winlesky Burham and if you have any corrections, please contact him and CC it to me so i can update this post ...
-
Recently I was locked out of a customer-provided laptop with their development environment, and access to their source code repository via v...