Guide to Unix/Commands/Process Management/Kill

From Wikibooks, open books for an open world
Jump to: navigation, search

kill is a unix command used to send unix signals to running processus. By default it will try to send the TERM signal which will ask the process to stop it works hence the name kill.

Available signals are available from the kill command:

$ kill -l
 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL
 5) SIGTRAP      6) SIGABRT      7) SIGBUS       8) SIGFPE
 9) SIGKILL     10) SIGUSR1     11) SIGSEGV     12) SIGUSR2
13) SIGPIPE     14) SIGALRM     15) SIGTERM     17) SIGCHLD
18) SIGCONT     19) SIGSTOP     20) SIGTSTP     21) SIGTTIN
22) SIGTTOU     23) SIGURG      24) SIGXCPU     25) SIGXFSZ
26) SIGVTALRM   27) SIGPROF     28) SIGWINCH    29) SIGIO
30) SIGPWR      31) SIGSYS      33) SIGRTMIN    34) SIGRTMIN+1
35) SIGRTMIN+2  36) SIGRTMIN+3  37) SIGRTMIN+4  38) SIGRTMIN+5
39) SIGRTMIN+6  40) SIGRTMIN+7  41) SIGRTMIN+8  42) SIGRTMIN+9
43) SIGRTMIN+10 44) SIGRTMIN+11 45) SIGRTMIN+12 46) SIGRTMIN+13
47) SIGRTMIN+14 48) SIGRTMIN+15 49) SIGRTMAX-15 50) SIGRTMAX-14
51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10
55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7  58) SIGRTMAX-6
59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX
$

You can send a given signal to all process having a given name or only to one process giving its PID. For example if you want to send signal SIGHUP to process 23154 :

kill -s SIGHUP 23154

If you want to send the same signal to all process named Apache :

kill -s SIGHUP Apache

You can also groups pid, the two commands above can be merged in:

kill -s SIGHUP 23154 Apache


Issuing signals, you can control your system a bit more. Say you have an ongoing script, running under PID 12345, which use a lot of your cpu and you would like to "pause" it while doing something else. You can issue your process the STOP signal:

kill -s STOP 12345

To later resume it, you will ask the process to CONTinue its work:

kill -s CONT 12345

Example:

We will start a simple process (sleep) in the background (&). The sleep process does nothing but waiting for the given number of seconds (in our case 360 seconds or 6 minutes):

$ sleep 360 &
[1] 7551
$

The shell give us back the PID of the process (7551). Let it run a bit then check its status with the ps command:

$ ps -o fname,pid,stat,etime
COMMAND    PID STAT     ELAPSED
bash      7425 Ss         04:47
sleep     7551 S          00:58
ps        7600 R+   49710-06:27:47
$

Now we ask our process to pause its work:

$  kill -s STOP 7551

Press enter to update your output buffer and the shell give you the confirmation:

$
[1]+  Stopped                 sleep 360
$

Sometime later, our process is still stopped:

$ ps -o fname,pid,stat,etime
COMMAND    PID STAT     ELAPSED
bash      7425 Ss         10:37
sleep     7551 T          06:05
ps        7719 R+   49710-06:27:47
$

You can notice that 6 minutes elapsed since the start although the process should have ended after 5 minutes (remember we used "sleep 360").

Now we want to tell the process to continue its operation:

$ kill -s CONT 7551

Update your buffer by pressing enter:

$
[1]+  Done                    sleep 360
$

Blam the process is finished. When you issued the CONT signal the process resumed and checked the current time, as more than 360 seconds have been elapsed it ended itself.