Linux_kill

kill – send a signal to a process

In fact, kill is to send a signal to a process id. The signal sent by default is SIGTERM, and the signal sent by kill -9 is SIGKILL, which is exit. The exit signal will not be blocked by the system, so kill -9 can kill the process smoothly. Of course you can also use kill to send other signals to the process.

killall – kill processes by name

That is, kill the process by specifying the process name.

 

kill use instance

Example 1: List all signal names

Order:

kill -l

output:

[root@localhost test6]# 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 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

illustrate:

Only the ninth signal (SIGKILL) can unconditionally terminate the process, and other signal processes have the right to ignore it. The following are commonly used signals:

HUP 1 terminal disconnection

INT 2 Interrupt (same as Ctrl + C)

QUIT 3 to quit (same as Ctrl + \)

TERM 15 Terminate

KILL 9 Forced Termination

CONT 18 Continue (opposite of STOP, fg/bg command)

STOP 19 Pause (same as Ctrl + Z)

Example 2: Get the value of the specified signal

Order:

output:

[root@localhost test6]# kill -l KILL

9[root@localhost test6]# kill -l SIGKILL

9[root@localhost test6]# kill -l TERM

15[root@localhost test6]# kill -l SIGTERM

15[root@localhost test6]#

illustrate:

Example 3: First use ps to find the process, then kill it with kill

Order:

kill 3268

output:

[root@localhost test6]# ps -ef|grep vim 

root      3268  2884  0 16:21 pts/1    00:00:00 vim install.log

root      3370  2822  0 16:21 pts/0    00:00:00 grep vim

[root@localhost test6]# kill 3268 

[root@localhost test6]# kill 3268 

-bash: kill: (3268) - no such process

[root@localhost test6]#

illustrate:

Example 4: Kill the process completely

Order:

kill –9 3268 

output:

[root@localhost test6]# ps -ef|grep vim 

root      3268  2884  0 16:21 pts/1    00:00:00 vim install.log

root      3370  2822  0 16:21 pts/0    00:00:00 grep vim

[root@localhost test6]# kill –9 3268 

[root@localhost test6]# kill 3268 

-bash: kill: (3268) - no such process

[root@localhost test6]#

illustrate:

Example 5: Kill all processes of the specified user

Order:

kill -9 $ (ps -ef | grep peidalinux)

kill -u peidalinux

output:

[root@localhost ~]# kill -9 $(ps -ef | grep peidalinux) 

[root@localhost ~]# kill -u peidalinux

illustrate:

Method 1, filter out the hnlinux user process and kill it

Example 6: The init process is unkillable

Order:

kill -9 1

output:

[root@localhost ~]# ps -ef|grep init

root         1     0  0 Nov02 ?        00:00:00 init [3]                  

root     17563 17534  0 17:37 pts/1    00:00:00 grep init

[root@localhost ~]# kill -9 1

[root@localhost ~]# kill -HUP 1

[root@localhost ~]# ps -ef|grep init

root         1     0  0 Nov02 ?        00:00:00 init [3]                  

root     17565 17534  0 17:38 pts/1    00:00:00 grep init

[root@localhost ~]# kill -KILL 1

[root@localhost ~]# ps -ef|grep init

root         1     0  0 Nov02 ?        00:00:00 init [3]                  

root     17567 17534  0 17:38 pts/1    00:00:00 grep init

[root@localhost ~]#

illustrate:

init is one of the indispensable programs in Linux system operation. The so-called init process, it is a user-level process started by the kernel. After the kernel starts itself (has been loaded into memory, started to run, and has initialized all device drivers and data structures, etc.), it completes the boot process by starting a user-level program init. So, init is always the first process (its process number is always 1). All other processes are descendants of the init process. The init process is not killable

 

Notice:

1. The kill command can have the signal number option or not. If there is no signal number, the kill command issues a kill signal (15), which can be caught by the process, allowing the process to clean up and free resources before exiting. You can also use kill to send specific signals to a process. E.g:

kill -2 123

Its effect is equivalent to pressing Ctrl+C while the process with PID 123 is running in the foreground. However, normal users can only use the kill command without the signal parameter or use the -9 signal at most.

2. Kill can take the process ID number as a parameter. When sending signals to these processes with kill, you must be the owner of these processes. If you try to revoke a process that does not have permission to revoke or revoke a process that does not exist, you will get an error message.

3. Multiple processes can be signaled or terminated.

4. When kill successfully sends the signal, the shell will display the termination information of the process on the screen. Sometimes this information doesn't show up right away, but only when the shell's command prompt reappears when the Enter key is pressed.

5. It should be noted that the signal causes the process to be forcibly terminated, which often brings some side effects, such as data loss or the terminal cannot be restored to a normal state. Care must be taken when sending signals, and the kill signal (9) should be used only as a last resort, as the process cannot catch it in the first place. To cancel all background jobs, type kill 0. Because some commands running in the background start multiple processes, it can be cumbersome to track down and find the PIDs of all the processes to kill. At this time, using kill 0 to terminate all processes started by the current shell is an effective method.

 

Manual OF KILL

Attachment 1:

KILL(1)                    Linux Programmer’s Manual                   KILL(1)

 

NAME

       kill - terminate a process

 

SYNOPSIS

       kill [ -s signal | -p ] [ -a ] [ -- ] pid ...

       kill -l [ signal ]

 

DESCRIPTION

       The  command  kill  sends the specified signal to the specified process or process group.  If no signal is specified, the TERM signal is sent.  The TERM signal will kill pro-

       cesses which do not catch this signal.  For other processes, it may be necessary to use the KILL (9) signal, since this signal cannot be caught.

 

       Most modern shells have a builtin kill function, with a usage rather similar to that of the command described here. The ‘-a’ and ‘-p’ options, and the possibility to  specify

       pids by command name is a local extension.

 

OPTIONS

       pid... Specify the list of processes that kill should signal.  Each pid can be one of five things:

 

              n      where n is larger than 0.  The process with pid n will be signaled.

 

              0      All processes in the current process group are signaled.

 

              -1     All processes with pid larger than 1 will be signaled.

 

              -n     where  n  is  larger  than  1.   All processes in process group n are signaled.  When an argument of the form ‘-n’ is given, and it is meant to denote a process

                     group, either the signal must be specified first, or the argument must be preceded by a ‘--’ option, otherwise it will be taken as the signal to send.

 

              commandname

                     All processes invoked using that name will be signaled.

 

       -s signal

              Specify the signal to send.  The signal may be given as a signal name or number.

 

       -l     Print a list of signal names.  These are found in /usr/include/linux/signal.h

 

       -a     Do not restrict the commandname-to-pid conversion to processes with the same uid as the present process.

 

       -p     Specify that kill should only print the process id (pid) of the named processes, and not send any signals.

 

SEE ALSO

       bash(1), tcsh(1), kill(2), sigvec(2), signal(7)

 

AUTHOR

       Taken from BSD 4.4.  The ability to translate process names to process ids was added by Salvatore Valente <[email protected]>.

 

附2:

 

KILL(1)                    Linux User's Manual                   KILL(1)

NAME

       kill - send a signal to a process

SYNOPSIS

       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 avail‐

       able 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

       output. A PID of -1 is special; it indicates all processes except

       the kill process itself and init.

SIGNALS

       The  signals  listed  below  may  be available for use with kill.

       When known constant, numbers and default behavior are shown.

       Name     Num   Action    Description

       0          0   n/a       exit code indicates if a signal may be sen

t

       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

       WINCH          ignore

       CHLD           ignore

       URG            ignore

       TSTP           stop      might interact with the shell

       TTIN           stop      might interact with the shell

       TTOU           stop      might interact with the shell

       STOP           stop      cannot be blocked

       CONT           restart   continue if stopped, otherwise ignore

       ABRT       6   core

       FPE        8   core

       ILL        4   core

       QUIT       3   core

       SEGV      11   core

       TRAP       5   core

       SYS            core      might not be implemented

       EMT            core      might not be implemented

       BUS            core      core dump might fail

       XCPU           core      core dump might fail

       XFSZ           core      core dump might fail

NOTES

       Your shell (command line interpreter) may have  a  built-in  kill

       command.   You  may  need  to  run  the command described here as

       /bin/kill to solve the conflict.

EXAMPLES

       kill -9 -1

              Kill all processes you can kill.

       kill -l 11

              Translate number 11 into a signal name.

       kill -L

              List the available signal choices in a nice table.

       kill 123 543 2341 3453

              Send the default signal, SIGTERM, to all those processes.

SEE ALSO

       pkill(1), skill(1), kill(2), renice(1), nice(1), signal(7),  kil‐

       lall(1).

STANDARDS

       This  command  meets appropriate standards. The -L flag is Linux-

       specific.

AUTHOR

       Albert  Cahalan  <[email protected]>  wrote  kill  in  1999  to

       replace  a  bsdutils  one  that  was not standards compliant. The

       util-linux one might also work correctly.

       Please send bug reports to <[email protected]>

Linux                       November 21, 1999                    KILL(1)

 

附3:

kill 信号列表

# kill -l

 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL

 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE

 9) SIGKILL10) SIGUSR111) SIGSEGV12) SIGUSR2

13) SIGPIPE14) SIGALRM15) SIGTERM16) SIGSTKFLT

17) SIGCHLD18) SIGCONT19) SIGSTOP20) SIGTSTP

21) SIGTTIN22) SIGTTOU23) SIGURG24) SIGXCPU

25) SIGXFSZ26) SIGVTALRM27) SIGPROF28) SIGWINCH

29) SIGIO30) SIGPWR31) SIGSYS34) SIGRTMIN

35) SIGRTMIN+136) SIGRTMIN+237) SIGRTMIN+338) SIGRTMIN+4

39) SIGRTMIN+540) SIGRTMIN+641) SIGRTMIN+742) SIGRTMIN+8

43) SIGRTMIN+944) SIGRTMIN+1045) SIGRTMIN+1146) SIGRTMIN+12

47) SIGRTMIN+1348) SIGRTMIN+1449) SIGRTMIN+1550) SIGRTMAX-14

51) SIGRTMAX-1352) SIGRTMAX-1253) SIGRTMAX-1154) SIGRTMAX-10

55) SIGRTMAX-956) SIGRTMAX-857) SIGRTMAX-758) SIGRTMAX-6

59) SIGRTMAX-560) SIGRTMAX-461) SIGRTMAX-362) SIGRTMAX-2

63) SIGRTMAX-164) SIGRTMAX

列表中,编号为1 ~ 31的信号为传统UNIX支持的信号,是不可靠信号(非实时的),编号为32 ~ 63的信号是后来扩充的,称做可靠信号(实时信号)。不可靠信号和可靠信号的区别在于前者不支持排队,可能会造成信号丢失,而后者不会。

下面我们对编号小于SIGRTMIN的信号进行讨论。

1) SIGHUP

本信号在用户终端连接(正常或非正常)结束时发出, 通常是在终端的控制进程结束时, 通知同一session内的各个作业, 这时它们与控制终端不再关联。

登录Linux时,系统会分配给登录用户一个终端(Session)。在这个终端运行的所有程序,包括前台进程组和后台进程组,一般都属于这个Session。当用户退出Linux登录时,前台进程组和后台有对终端输出的进程将会收到SIGHUP信号。这个信号的默认操作为终止进程,因此前台进 程组和后台有终端输出的进程就会中止。不过可以捕获这个信号,比如wget能捕获SIGHUP信号,并忽略它,这样就算退出了Linux登录,wget也 能继续下载。

此外,对于与终端脱离关系的守护进程,这个信号用于通知它重新读取配置文件。

2) SIGINT

程序终止(interrupt)信号, 在用户键入INTR字符(通常是Ctrl-C)时发出,用于通知前台进程组终止进程。

3) SIGQUIT

和SIGINT类似, 但由QUIT字符(通常是Ctrl-\)来控制. 进程在因收到SIGQUIT退出时会产生core文件, 在这个意义上类似于一个程序错误信号。

4) SIGILL

执行了非法指令. 通常是因为可执行文件本身出现错误, 或者试图执行数据段. 堆栈溢出时也有可能产生这个信号。

5) SIGTRAP

由断点指令或其它trap指令产生. 由debugger使用。

6) SIGABRT

调用abort函数生成的信号。

7) SIGBUS

非法地址, 包括内存地址对齐(alignment)出错。比如访问一个四个字长的整数, 但其地址不是4的倍数。它与SIGSEGV的区别在于后者是由于对合法存储地址的非法访问触发的(如访问不属于自己存储空间或只读存储空间)。

8) SIGFPE

在发生致命的算术运算错误时发出. 不仅包括浮点运算错误, 还包括溢出及除数为0等其它所有的算术的错误。

9) SIGKILL

用来立即结束程序的运行. 本信号不能被阻塞、处理和忽略。如果管理员发现某个进程终止不了,可尝试发送这个信号。

10) SIGUSR1

留给用户使用

11) SIGSEGV

试图访问未分配给自己的内存, 或试图往没有写权限的内存地址写数据.

12) SIGUSR2

留给用户使用

13) SIGPIPE

管道破裂。这个信号通常在进程间通信产生,比如采用FIFO(管道)通信的两个进程,读管道没打开或者意外终止就往管道写,写进程会收到SIGPIPE信号。此外用Socket通信的两个进程,写进程在写Socket的时候,读进程已经终止。

14) SIGALRM

时钟定时信号, 计算的是实际的时间或时钟时间. alarm函数使用该信号.

15) SIGTERM

程序结束(terminate)信号, 与SIGKILL不同的是该信号可以被阻塞和处理。通常用来要求程序自己正常退出,shell命令kill缺省产生这个信号。如果进程终止不了,我们才会尝试SIGKILL。

17) SIGCHLD

子进程结束时, 父进程会收到这个信号。

如果父进程没有处理这个信号,也没有等待(wait)子进程,子进程虽然终止,但是还会在内核进程表中占有表项,这时的子进程称为僵尸进程。这种情 况我们应该避免(父进程或者忽略SIGCHILD信号,或者捕捉它,或者wait它派生的子进程,或者父进程先终止,这时子进程的终止自动由init进程来接管)。

18) SIGCONT

让一个停止(stopped)的进程继续执行. 本信号不能被阻塞. 可以用一个handler来让程序在由stopped状态变为继续执行时完成特定的工作. 例如, 重新显示提示符...

19) SIGSTOP

停止(stopped)进程的执行. 注意它和terminate以及interrupt的区别:该进程还未结束, 只是暂停执行. 本信号不能被阻塞, 处理或忽略.

20) SIGTSTP

停止进程的运行, 但该信号可以被处理和忽略. 用户键入SUSP字符时(通常是Ctrl-Z)发出这个信号

21) SIGTTIN

当后台作业要从用户终端读数据时, 该作业中的所有进程会收到SIGTTIN信号. 缺省时这些进程会停止执行.

22) SIGTTOU

类似于SIGTTIN, 但在写终端(或修改终端模式)时收到.

23) SIGURG

有"紧急"数据或out-of-band数据到达socket时产生.

24) SIGXCPU

超过CPU时间资源限制. 这个限制可以由getrlimit/setrlimit来读取/改变。

25) SIGXFSZ

当进程企图扩大文件以至于超过文件大小资源限制。

26) SIGVTALRM

虚拟时钟信号. 类似于SIGALRM, 但是计算的是该进程占用的CPU时间.

27) SIGPROF

类似于SIGALRM/SIGVTALRM, 但包括该进程用的CPU时间以及系统调用的时间.

28) SIGWINCH

窗口大小改变时发出.

29) SIGIO

文件描述符准备就绪, 可以开始进行输入/输出操作.

30) SIGPWR

Power failure

31) SIGSYS

非法的系统调用。

在以上列出的信号中,程序不可捕获、阻塞或忽略的信号有:SIGKILL,SIGSTOP

不能恢复至默认动作的信号有:SIGILL,SIGTRAP

默认会导致进程流产的信号有:SIGABRT,SIGBUS,SIGFPE,SIGILL,SIGIOT,SIGQUIT,SIGSEGV,SIGTRAP,SIGXCPU,SIGXFSZ

默认会导致进程退出的信号有:SIGALRM,SIGHUP,SIGINT,SIGKILL,SIGPIPE,SIGPOLL,SIGPROF,SIGSYS,SIGTERM,SIGUSR1,SIGUSR2,SIGVTALRM

默认会导致进程停止的信号有:SIGSTOP,SIGTSTP,SIGTTIN,SIGTTOU

默认进程忽略的信号有:SIGCHLD,SIGPWR,SIGURG,SIGWINCH

此外,SIGIO在SVR4是退出,在4.3BSD中是忽略;SIGCONT在进程挂起时是继续,否则是忽略,不能被阻塞

 

kill -9

Perl语言专家Randal Schwartz在一篇短文里这样写:

"no no no.不要使用kill -9. 

它没有给进程留下善后的机会:

1) 关闭socket链接

2) 清理临时文件

3) 将自己将要被销毁的消息通知给子进程

4) 重置自己的终止状态

等等。

通常,应该发送15,等一两秒钟,如果没效果,发送2,如果还不行,发送1。如果还不行,那你应该把那个程序删掉,因为那个程序写的太烂了!

不要使用kill -9。不要用收割机来修剪花盆里的花"

总之,在使用kill -9前,你应该先使用kill -15,给目标进程一个清理善后工作的机会。(进程也许无法捕捉或直接忽略SIGKILL信号,但它们可以,通常也是能够捕捉SIGTERM信号的。)如果你不留机会让进程完成清理工作,它们会留下一些不完整的文件或状态,当系统重启时,程序将无法理解这些状态。

strace/truss,ltrace和gdb都是查看一个卡住的进程因何卡住的好工具。(Solaris里的truss -u非常好用;)Solaris里还有一些非常有用的/proc相关的工具,有一些已经被移植到了Linux上。

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327075637&siteId=291194637