Sunday, 29 January 2012

kill

kill

kill is used to send a signal to the process, though the name suggests that it is used only to terminate a process. But, kill helps to terminate a process more often than not. When some process is run unexpectedly or accidentally, it might have to be aborted ,for which kill helps just like CTRL+ALT+DEL in windows .

Example 1)

ps command gives you the process id s and other details of various commands, if you want to stop any command, just check the corresponding PID of the process(the command) which you want to terminate ,then use kill to stop that process.

Consider the output of ps,

/home/b6789$ps -fu b6789

UID PID PPID C STIME TTY TIME CMD

b6789 352266 1 0 05:32:38 - 0:14 java /usr/bin/wc –l

b6789 368734 1 0 05:32:42 - 0:07 [wc]

------b6789 372912 120 0 05:32:43 - 5:10 java /usr/bin/java/dev2.java

b6789 409806 1 0 05:32:39 - 0:13 java /usr/bin/java/start-atm.java

this process is consuming cpu and has to be terminated,

/home/b6789$kill 372912

This sends the signal SIGTERM to the process 372912, and terminates it.

Sometimes the process may refuse to terminate just by SIGTERM, in that case use SIGKILL signal(kill -9)

/home/b6789$kill -9 372912

This signal cannot be ignored . You can kill any of the process initiated by you, i:e from your user id this way .A root user has the privilege of killing any user’s processes .

Example 2)

You can also get the specific PID for your command without having to search it using grep and awk along with ps. Then kill the PID which you have retrieved.

You can also use xargs to kill process of a particular type all at once .

Example 3)

There is a method to kill all the processes initiated by you,

/home/b6789$kill -9 -1

This will kill all the processes of your user id and also closes your login session. It is a very dangerous command ,so think thrice and check whether any required process is still running using ps before you press enter.

You can send many kinds of signals to a process(not all) by using kill -<signal> PID. The various signal numbers and their names are listed below (for AIX) in detail .A short list can also be viewed