您的位置:首页 > 运维架构 > Linux

我使用过的Linux命令之kill - 终止进程/发送信号

2020-01-15 02:07 309 查看

用途说明

kill命令用于终止指定的进程(terminate a process),是Unix/Linux下进程管理的常用命令。通常,我们在需要终止某个或某些进程时,先使用ps/pidof/pstree/top等工具获取进程PID,然后使用kill命令来杀掉该进程。kill命令的另外一个用途就是向指定的进程或进程组发送信号(The  command kill sends the specified signal to the specified process or process group),或者确定进程号为PID的进程是否还在。比如,有许多程序都把SIGHUP信号作为重新读取配置文件的触发条件。

在Bash中,kill命令是个Shell内建命令,为什么呢?原因有两个:第一个原因,Bash可以进行任务管理(&,Ctrl+Z,bg,fg,kill %jobid等指令),如果kill只是一个外部命令,那么在需要终止某个任务时就会很难办,因为任务是与终端关联的,反过来,如果kill命令是shell内建命令,就可以很方便的采用kill %jobid的形式去杀掉指定任务(似乎这个原因还不是很充分);第二个原因,是更加重要的,如果要杀掉某个进程还得来启动一个名为kill的子进程,那么在达到允许创建的进程数量上限时,连kill进程本身都无法创建了,还怎么来杀掉其他进程呢!(Kill is a shell builtin for two reasons: it allows job IDs to be used instead of process IDs, and, if you have reached the limit on processes that you can create, you don't have to start a process to kill another one.)

 

常用参数

格式:kill <pid>

格式:kill -TERM <pid>

发送SIGTERM信号到指定进程,如果进程没有捕获该信号,则进程终止(If no signal is specified, the TERM signal is sent.  The TERM signal will kill processes which do not catch this signal.)

 

格式:kill -l

列出所有信号名称(Print a list of signal names.  These are found in /usr/include/linux/signal.h)。只有第9种信号(SIGKILL)才可以无条件终止进程,其他信号进程都有权利忽略。下面是常用的信号:

HUP     1    终端断线
INT       2    中断(同 Ctrl + C)
QUIT    3    退出(同 Ctrl + \)
TERM    15    终止
KILL      9    强制终止
CONT   18    继续(与STOP相反, fg/bg命令)
STOP    19    暂停(同 Ctrl + Z)

 

格式:kill -l <signame>

显示指定信号的数值。

 

格式:kill -9 <pid>

格式:kill -KILL <pid>

强制杀掉指定进程,无条件终止指定进程。

 

格式:kill %<jobid>

格式:kill -9 %<jobid>

杀掉指定的任务(使用jobs命令可以列出)

 

使用示例

示例一 kill命令是什么类型的命令

[root@new55 ~]# type -a kill 
kill is a shell builtin
kill is /bin/kill
kill is /usr/bin/kill

[root@new55 ~]# help kill 
kill: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
    Send the processes named by PID (or JOBSPEC) the signal SIGSPEC.  If
    SIGSPEC is not present, then SIGTERM is assumed.  An argument of `-l'
    lists the signal names; if arguments follow `-l' they are assumed to
    be signal numbers for which names should be listed.  Kill is a shell
    builtin for two reasons: it allows job IDs to be used instead of
    process IDs, and, if you have reached the limit on processes that
    you can create, you don't have to start a process to kill another one.

[root@new55 ~]#

 

示例二 列出所有信号名称

[root@new55 ~]# 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
[root@new55 ~]#

/usr/include/linux/signal.h 写道 #define SIGHUP 1
#define SIGINT 2
#define SIGQUIT 3
#define SIGILL 4
#define SIGTRAP 5
#define SIGABRT 6
#define SIGIOT 6
#define SIGBUS 7
#define SIGFPE 8
#define SIGKILL 9
#define SIGUSR1 10
#define SIGSEGV 11
#define SIGUSR2 12
#define SIGPIPE 13
#define SIGALRM 14
#define SIGTERM 15
#define SIGSTKFLT 16
#define SIGCHLD 17
#define SIGCONT 18
#define SIGSTOP 19
#define SIGTSTP 20
#define SIGTTIN 21
#define SIGTTOU 22
#define SIGURG 23
#define SIGXCPU 24
#define SIGXFSZ 25
#define SIGVTALRM 26
#define SIGPROF 27
#define SIGWINCH 28
#define SIGIO 29
#define SIGPOLL SIGIO
/*
#define SIGLOST 29
*/
#define SIGPWR 30
#define SIGSYS 31
#define SIGUNUSED 31

/* These should not be considered constants from userland. */
#define SIGRTMIN 32
#define SIGRTMAX _NSIG

 

示例三 得到指定信号的数值

[root@new55 ~]# kill -l KILL 
9
[root@new55 ~]# kill -l SIGKILL 
9
[root@new55 ~]# kill -l TERM 
15
[root@new55 ~]# kill -l SIGTERM 
15
[root@new55 ~]#

 

示例四 init进程是不可杀的!

init是Linux系统操作中不可缺少的程序之一。所谓的init进程,它是一个由内核启动的用户级进程。内核自行启动(已经被载入内存,开始运行,并已初始化所有的设备驱动程序和数据结构等)之后,就通过启动一个用户级程序init的方式,完成引导进程。所以,init始终是第一个进程(其进程编号始终为1)。 其它所有进程都是init进程的子孙。

[root@new55 ~]# kill -HUP 1 
[root@new55 ~]# kill -9 1 
[root@new55 ~]# kill -KILL 1

系统依然在运行。 
[root@new55 ~]#

 

示例五 先用ps查找进程,然后用kill杀掉

[root@new55 ~]# ps -ef|grep vim 
root      3368  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@new55 ~]# kill 3368 
[root@new55 ~]# kill 3368 
-bash: kill: (3368) - 没有那个进程
[root@new55 ~]#

 

示例六 杀掉指定任务

[root@new55 ~]# tail -f install.log 
瀹夎 scim-bridge-gtk-0.4.5-9.el5.i386
瀹夎 scim-pinyin-0.5.91-16.el5.i386
瀹夎 scim-chewing-0.3.1-11.el5.i386
瀹夎 scim-chinese-standard-0.0.2-1.el5.i386
瀹夎 scim-tables-0.5.6-7.i386
瀹夎 scim-qtimm-0.9.4-5.i386
瀹夎 scim-tables-chinese-0.5.6-7.i386
瀹夎 fonts-chinese-3.02-12.el5.noarch
瀹夎 isdn4k-utils-3.2-56.el5.i386
瀹夎 stardict-2.4.5-5.i386
Ctrl+Z 
[1]+  Stopped                 tail -f install.log
[root@new55 ~]# kill %1 
[root@new55 ~]# kill %1 
-bash: kill: (3379) - 没有那个进程
[1]+  已终止                  tail -f install.log
[root@new55 ~]#

 

 

转载于:https://my.oschina.net/u/1024573/blog/513788

  • 点赞
  • 收藏
  • 分享
  • 文章举报
chuchaner7283 发布了0 篇原创文章 · 获赞 0 · 访问量 1462 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: