Linux killall command

作用

killall - kill processes by name

killall sends a signal to all processes running any of the specified commands. If no signal name is specified, SIGTERM is sent. Signals can be specified either by name (e.g. -HUP or -SIGHUP) or by number (e.g. -1) or by option -s.

If the command name is not regular expression (option -r) and contains a slash (/), processes executing that particular file will be selected for killing, independent of their name.

killall returns a zero return code if at least one process has been killed for each listed command, or no commands were listed and at least one process matched the -u and -Z search criteria. killall returns non-zero otherwise.

A killall process never kills itself (but may kill other killall processes)

用法

命令格式

1
2
3
killall [-Z CONTEXT] [-u USER] [ -eIgiqrvw ] [ -SIGNAL ] NAME...
killall -l, --list
killall -V, --version

命令参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
-e,--exact          require exact match for very long names
-I,--ignore-case case insensitive process name match
-g,--process-group kill process group instead of process
-y,--younger-than kill processes younger than TIME
-o,--older-than kill processes older than TIME
-i,--interactive ask for confirmation before killing
-l,--list list all known signal names
-q,--quiet don't print complaints
-r,--regexp interpret NAME as an extended regular expression
-s,--signal SIGNAL send this signal instead of SIGTERM
-u,--user USER kill only process(es) running as USER
-v,--verbose report if the signal was successfully sent
-V,--version display version information
-w,--wait wait for processes to die
-Z,--context REGEXP kill only process(es) having context
(must precede other arguments)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
-e,--exact 精准匹配。由于命令名长于15个字符时,会杀死匹配前15个字符的所有进程。此时需要使用-e选项进行精准匹配。
-g,--process-group 杀死属于该进程组的所有进程。
-i,--interactive 交互式,杀死进程前请求确认
-I,--ignore-case 进程名匹配不区分大小写
-l,--list 列出所有信号名
-o,--older-than 仅匹配指定时间之前(在指定时间之前开始)的进程。单位s、m、h、d、w(周)、M、y
-q,--quiet 静默模式,不报错
-r,--regexp 使用扩展正则表达式解释name
-s,--signal 发送指定信号等同[-signal]
-u.--user 仅终止指定用户拥有的进程。命令名是可选的。
-v,--verbose 报告是否发送成功
-V,version 显示版本
-w,--wait 等待killall执行完成,每秒检测一次,直到完成返回。如果信号被忽略或不起作用,或停留在僵尸状态,killall会永久等待。
-y,--younger-than 仅匹配指定时间之后的进程,与-o选项相反。
-z,--context (仅限SELinux)指定环境(变量):仅杀死含有与指定扩展正则表达式匹配的环境(变量)的进程。必须在其他参数之前,命令名是可选的。

用法示例

查看版本

1
2
3
4
5
6
7
8
[root@hadoop-centos-01 ~]# killall -V
killall (PSmisc) 22.20
Copyright (C) 1993-2012 Werner Almesberger and Craig Small

PSmisc comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it under
the terms of the GNU General Public License.
For more information about these matters, see the files named COPYING.

查看信号列表

1
2
3
4
[root@hadoop-centos-01 ~]# killall -l
HUP INT QUIT ILL TRAP ABRT IOT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM
STKFLT CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH IO PWR SYS
UNUSED

常用的信号说明:

  • HUP(1,终端断线),

  • INT(2,中断,同<Ctrl>+c)

  • QUIT(3,退出,同<Ctrl>+\)

  • KILL(9,强制终止)

  • TERM(15,缺省信号,终止,正常结束)

  • CONT(18,继续,同fg/bg命令)

  • STOP(19,停止)

  • TSTP(20,暂停,同<Ctrl>+Z)

杀死所有同名进程

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@hadoop-centos-01 ~]# ps -aux | grep python
root 32618 0.0 0.0 112708 980 pts/0 S+ 07:18 0:00 grep --color=auto python

# 启动一个python后台进程
[root@hadoop-centos-01 ~]# ps -aux | grep python
root 32619 0.2 0.5 130456 5040 pts/3 S+ 07:18 0:00 python
root 32621 0.0 0.0 112708 980 pts/0 S+ 07:18 0:00 grep --color=auto python

# 执行 killall python 命令, 等效于 killall -15 python
[root@hadoop-centos-01 ~]# killall python
# 再次过滤发现该进程以终止
[root@hadoop-centos-01 ~]# ps -aux | grep python
root 32624 0.0 0.0 112708 980 pts/0 S+ 07:18 0:00 grep --color=auto python

向进程发送指定信号

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@hadoop-centos-01 ~]# ps -aux | grep python
root 32670 0.1 0.5 130456 5044 pts/3 T 07:22 0:00 python
root 32684 0.0 0.0 112708 980 pts/0 S+ 07:23 0:00 grep --color=auto python

[root@hadoop-centos-01 ~]# killall python; ps -aux | grep python
root 32670 0.0 0.5 130456 5044 pts/3 T 07:22 0:00 python
root 32734 0.0 0.0 112708 980 pts/0 S+ 07:26 0:00 grep --color=auto python

# 正常无法终止进程时,需要强制终止,需发送指定信号,终止进程
[root@hadoop-centos-01 ~]# killall -9 python
Python 2.7.15 (default, May 17 2019, 07:26:27)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
[1]+ Stopped python

[root@hadoop-centos-01 ~]# ps -aux | grep python
root 32766 0.0 0.0 112708 980 pts/0 S+ 07:29 0:00 grep --color=auto python

杀掉所有的登录后的shell

1
2
3
4
5
6
[root@hadoop-centos-01 ~]# killall -9 bash

Session stopped
- Press <return> to exit tab
- Press R to restart session
- Press S to save terminal output to file
-------------本文结束感谢您的阅读-------------