linux 命令详解

本文主要内容来自Linux man 手册

命令名称:

kill 向任务发送一个信号

命令用法:

kill [选项] <pid> [...]            []表示可选参数

命令概述:

kill发送的信号默认为TERM(终止),使用-l或-L列出可选的信号,特别有用的信号包括HUP、INT、
KILL、STOP、CONT和0,可通过三种方式指定信号:-9,-SIGKILL或-KILL。负PID值可用于选择整个
进程组,请参考ps命令输出中的PGID列,PID为-1比较特殊,它表示除kill进程本身和init之外的所有
进程。

命令参数:

<pid> [...]
	向<pid>发送信号

-<signal>
-s <signal>
--signal <signal>
	指定发送的信号,信号可以通过名字或编号指定,信号的介绍可以参考signal手册第7页
	(man 7 signal)

-l,--list [signal]
	列举所有信号的名称,此选项具有可选参数,可将将信号编号转换为信号名称,或以其他方式转换。

-L,--table
	以表的格式列举信号名
	注:如果你系统内置了kill命令,没有-L选项,需要运行/bin/kill

示例:

1. kill <pid> […]

向pid发送SIGTERM信号,终止进程。

xiaohui@ubuntu:~/work$ ps
   PID TTY          TIME CMD
 12723 pts/2    00:00:00 bash
 13684 pts/2    00:00:38 test.out
 13688 pts/2    00:00:00 ps
xiaohui@ubuntu:~/work$ kill 13684
xiaohui@ubuntu:~/work$ ps
   PID TTY          TIME CMD
 12723 pts/2    00:00:00 bash
 13697 pts/2    00:00:00 ps
[1]+  已终止               ./test.out
xiaohui@ubuntu:~/work$ 

2. kill-<signal> <pid> […]

同-s <signal>和–signal <signal>,指定发送的信号
默认kill发送的是SIGTERM,相当于kill -15

xiaohui@ubuntu:~/work$ ps
   PID TTY          TIME CMD
 13272 pts/9    00:00:00 bash
 13809 pts/9    00:00:18 test.out
 13811 pts/9    00:00:00 ps
xiaohui@ubuntu:~/work$ kill -15 13809
xiaohui@ubuntu:~/work$ ps
   PID TTY          TIME CMD
 13272 pts/9    00:00:00 bash
 13812 pts/9    00:00:00 ps
[1]+  已终止               ./test.out
xiaohui@ubuntu:~/work$ 

我们也可以使用:-9,-SIGKILL或-KIL,这三个选项都代表KILL信号,作用是杀死进程

xiaohui@ubuntu:~/work$ ps
   PID TTY          TIME CMD
 13272 pts/9    00:00:00 bash
 13826 pts/9    00:00:25 test.out
 13833 pts/9    00:00:00 ps
xiaohui@ubuntu:~/work$ kill -9 13826
xiaohui@ubuntu:~/work$ ps
   PID TTY          TIME CMD
 13272 pts/9    00:00:00 bash
 13834 pts/9    00:00:00 ps
[1]+  已杀死               ./test.out
xiaohui@ubuntu:~/work$ 

3. kill -l [信号编号…]

列出所有信号或将信号编号对应的信号名称打印出来

xiaohui@ubuntu:~/work$ 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	
xiaohui@ubuntu:~/work$ kill -l 9 15
KILL
TERM
xiaohui@ubuntu:~/work$ 

4. kill -L

以制表符来分隔显示的信号名称,如果系统内置了kill命令,需要运行/bin/kill才能使用该选项。

xiaohui@ubuntu:~/work$ /bin/kill -L 1
 1 HUP      2 INT      3 QUIT     4 ILL      5 TRAP     6 ABRT     7 BUS
 8 FPE      9 KILL    10 USR1    11 SEGV    12 USR2    13 PIPE    14 ALRM
15 TERM    16 STKFLT  17 CHLD    18 CONT    19 STOP    20 TSTP    21 TTIN
22 TTOU    23 URG     24 XCPU    25 XFSZ    26 VTALRM  27 PROF    28 WINCH
29 POLL    30 PWR     31 SYS     
xiaohui@ubuntu:~/work$ 

man手册

以下为 kill 命令手册原文:

KILL(1)                          User Commands                         KILL(1)

NAME
       kill - send a signal to a process

SYNOPSIS
       kill [options] <pid> [...]

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 or -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.

OPTIONS
       <pid> [...]
              Send signal to every <pid> listed.

       -<signal>
       -s <signal>
       --signal <signal>
              Specify  the  signal to be sent.  The signal can be specified by
              using name or number.  The behavior of signals is  explained  in
              signal(7) manual page.

       -l, --list [signal]
              List  signal  names.   This  option has optional argument, which
              will convert signal number to signal name, or other way round.

       -L, --table
              List signal names in a nice table.

       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
       kill(2), killall(1), nice(1), pkill(1), renice(1), signal(7), skill(1)

STANDARDS
       This command meets appropriate standards. The  -L  flag  is  Linux-spe‐
       cific.

AUTHOR
       Albert  Cahalan  ⟨albert@users.sf.net⟩  wrote kill in 1999 to replace a
       bsdutils one that was not  standards  compliant.   The  util-linux  one
       might also work correctly.

REPORTING BUGS
       Please send bug reports to ⟨procps@freelists.org⟩

procps-ng                        October 2011                          KILL(1)

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐