本文发布时间: 2019-Mar-22
有时候我们使用kill无法杀掉一个进程,但是用kill -9却可以,why? 首先看一下kill的功能,man手册对kill描述如下:KILL(1) Linux User's Manual KILL(1)NAME kill - send a signal to a processSYNOPSIS kill [ -signal | -s signal ] pid ... kill [ -L | -V, --version ] kill -l [ signal ]DESCRIPTION The default signal for kill is TERM. Use -l or -L to list available signals. Particularly useful signals include HUP, INT, KILL, STOP, CONT, and 0. Alternate signals may be specified in three ways: -9 -SIGKILL -KILL. Negative PID values may be used to choose whole process groups; see the PGID column in ps command out‐ put. A PID of -1 is special; it indicates all processes except the kill process itself and init. 在NAME部分注明:kill的作用是向进程发送一个信号(并没有说是杀掉进程哈)。具体发送什么信号由后面接的参数决定。 那么为什么很多时候我们又可以杀掉进程?请看DESDCRIPTION部分: kill默认参数是TERM。也就是说,如果没指定具体的信号作为参数,则默认使用kill TERM pid。因此kill pid是可以杀掉一个进程。 那么问题来了,为什么有的时候kill不能杀掉进程,而kill -9就可以了? 《APUE》中关于信号的描述中有介绍,大多数信号可以被捕获的。而TERM信号就是在这个大多数里的,一些进程可能为了特殊的用途捕获了TERM信号,导致了你使用kill pid时无法杀掉进程。 另外《APUE》中也强调了,有两个信号不能被捕获,SIGKILL 和SIGSTOPNote that the two signals SIGKILL and SIGSTOP can’t be caught. 没错,kill -9 就是向进程发送SIGKILL信号。 kill的man手册:SIGNALS The signals listed below may be available for use with kill. When known constant, numbers and default behav‐ ior are shown. Name Num Action Description 0 0 n/a exit code indicates if a signal may be sent ALRM 14 exit HUP 1 exit INT 2 exit KILL 9 exit cannot be blocked PIPE 13 exit POLL exit PROF exit TERM 15 exit USR1 exit USR2 exit VTALRM exit STKFLT exit might not be implemented PWR ignore might exit on some systems …… ……


2020-Jul-13 01:13am