bash: pkill = `kill -9` ?
Blogs20112011-09-29
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:
- SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL
- SIGTRAP 6) SIGABRT 7) SIGBUS 8 ) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2
- SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT
- SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
- SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU
- SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH
- SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN
- SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4
- SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
- SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12
- SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14
- SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10
- SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6
- SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
- 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'