Amazon

Jan 6, 2010

Linux Tip: Linux monitor processes using kill

One of the tasks of an Administrators or even users of Linux server, workstations and desktop is to monitor running processes. Processes that are important to your production Linux servers. One of the command that I recently learn that can help in monitoring linux processes is the kill -0 command. But wait, kill means terminate the process. How can we monitor the process if we use the kill command?

When you refer to the manual/info of kill, 0 means no signal will be sent, it will not terminate the process. By combining return status(echo $?) with kill -0, you can check if a certain process is running or not.

Open a gnome-terminal and login as root or use sudo for Ubuntu user.
All processes running (UNIX daemon) are at /var/run directory. So when you want to check the process id(pid) of yum daemon, just look into the file /var/run/yum.pid.

    root@linux# cat /var/run/yum.pid
    1755
    root@linux#

Do the following command when checking the status of process:
   
    root@linux# kill -0 1755
    root@linux# echo $?
    0
    root@linux#


If the output of echo $? is 0, that means the process is running. Refer to my other post regarding $? variable here.

To shorten the above procedure, we can just do this:
   
    root@linux# kill -0 $(cat /var/run/yum.pid)
    root@linux# echo $?
    0
    root@linux# 


Daemon is not running ( yums.pid doesn't exist )

    root@linux# kill -0 $(cat /var/run/yums.pid)
    cat: /var/run/yums.pid: No such file or directory
    root@linux# echo $?
    1
    root@linux#

You can now monitor processes with kill -0 command. Just play around, it will help in some unexpected situations.

No comments:

Post a Comment