William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

bash: pkill = `kill -9` ?

kill -9

In Linux env, we usually kill process by using ‘kill -9‘ or ‘pkill‘. For example, to kill a ‘sleep’ process, there are 2 ways:

//1. pkill
$ pkill sleep
//2. or kill -9 "sleep's pid"
kill -9 `ps -ef|grep sleep|grep -v grep|awk '{print $2}'`

Are they the same? Why ‘kill -9‘?
‘-9’ means using Linux signal ‘SIGKILL’. The following is a list of Linux signal names:

 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     16) SIGSTKFLT
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      34) SIGRTMIN
35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3  38) SIGRTMIN+4
39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12
47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+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

The 9 is SIGKILL, means using SIGKILL to kill the current pid. According to ‘info SIGKILL‘ explaination:

 -- Macro: int SIGKILL
     The `SIGKILL' signal is used to cause immediate program
     termination.  It cannot be handled or ignored, and is therefore
     always fatal.  It is also not possible to block this signal.

     This signal is usually generated only by explicit request.  Since
     it cannot be handled, you should generate it only as a last
     resort, after first trying a less drastic method such as `C-c' or
     `SIGTERM'.  If a process does not respond to any other termination
     signals, sending it a `SIGKILL' signal will almost always cause it
     to go away.

     In fact, if `SIGKILL' fails to terminate a process, that by itself
     constitutes an operating system bug which you should report.

     The system will generate `SIGKILL' for a process itself under some
     unusual conditions where the program cannot possibly continue to
     run (even to run a signal handler).
   

pkill

pkill will send the specified signal (by default SIGTERM) to each process instead of listing them on stdout.
‘pkill sleep ‘ use SIGTERM to terminate the process.
While ‘kill -9 pid’ mean use SIGKILL to kill the process. They are different.

So by default: pkill = kill -15, not kill -9

However the following is equal:

 $ pkill -SIGKILL sleep
 $ kill -9 'sleep pid'
Advertisements

One response to “bash: pkill = `kill -9` ?

  1. powercashadvance.com 10/26/2011 at 3:30 pm

    What would many of us do minus the excellent thoughts you talk about on this web site? Who else comes with the patience to deal with important topics for the sake of common readers like me? My partner and i and my buddies are very lucky to have your website among the kinds we generally visit. It is hoped you know how considerably we enjoy your efforts! Best wishes through us all.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: