Linux 下使用 killall 命令终止进程的 8 大用法

Linux 的命令行提供很多命令来杀死进程。比如,你可以向 kill 命传递一个PID来杀死进程;pkill 命令使用一个正则表达式作为输入,所以和该模式匹配的进程都被杀死。

但是还有一个命令叫 killall ,默认情况下,它精确地匹配参数名,然后杀死匹配进程。在这篇文章中,我们将讨论有关这个命令的实际应用。

默认情况下,killall 命令将向一个/组进程发送一个 SIGTERM 信号,但是,也可以通过参数发送一个指定的信号。

下面我们通过例子详细介绍 killall 的 8 大用法。

1. 基本用法

假如我们 3 个进程在运行,分别是 hello1, hello2, hello3 ,现在我们想杀死 hello1 进程,可以直接使用如下方式:

killall hello1

运行的结果如下:

[_0_16_centos test]$ ps aux | grep hello
alvin    12061  0.0  0.0   4152   344 pts/0    S    14:41   0:00 ./hello1
alvin    12074  0.0  0.0   4152   344 pts/0    S    14:41   0:00 ./hello2
alvin    12084  0.0  0.0   4152   340 pts/0    S    14:41   0:00 ./hello3
alvin    12089  0.0  0.0 112648   964 pts/0    R+   14:41   0:00 grep --color=auto hello
[_0_16_centos test]$ killall hello1
[1]   Terminated              ./hello1
[_0_16_centos test]$ ps aux | grep hello
alvin    12074  0.0  0.0   4152   344 pts/0    S    14:41   0:00 ./hello2
alvin    12084  0.0  0.0   4152   340 pts/0    S    14:41   0:00 ./hello3
alvin    12170  0.0  0.0 112648   964 pts/0    R+   14:42   0:00 grep --color=auto hello

可以看到,hello1 进程已经被杀死了。

剩下的 hello2 和 hello3 进程,我们想一次性杀死他们,也就是批量杀死进程,可以如下操作:

[_0_16_centos test]$ killall hello*
hello: no process found
hello1: no process found
hello.c: no process found
[2]-  Terminated              ./hello2
[3]+  Terminated              ./hello3

如此,以 hello 开头的进程全部被干掉。

2. 终止某个用户所运行的进程

我们可以杀死以满足某个正则表达式的一组进程,同样的,我们也可以杀死某个用户运行的所有进程。

比如,用户 harry 现在运行如下几个进程:

[_0_16_centos test]$ ps aux | grep harry
root     13675  0.0  0.2 148236  5584 ?        Ss   14:55   0:00 sshd: harry [priv]
harry    13677  0.0  0.1 148236  2944 ?        S    14:55   0:00 sshd: /1
root     13678  0.0  0.2 148236  5444 ?        Ss   14:55   0:00 sshd: harry [priv]
harry    13680  0.0  0.1 148236  2252 ?        S    14:55   0:00 sshd: 
harry    13681  0.0  0.1  53228  2168 ?        Ss   14:55   0:00 /usr/libexec/openssh/sftp-server
harry    13694  0.0  0.1 116436  3252 pts/1    Ss+  14:55   0:00 -bash
harry    13948  0.0  0.0   4152   344 pts/1    S    14:57   0:00 ./hello1
harry    13952  0.0  0.0   4152   344 pts/1    S    14:57   0:00 ./hello2
harry    13959  0.0  0.0   4152   344 pts/1    S    14:57   0:00 ./hello3
alvin    14005  0.0  0.0 112648   964 pts/0    R+   14:58   0:00 grep --color=auto harry

我们现在想杀死 harry 所运行的所有进程,可以以如下方式操作:

killall -u harry

运行结果如下:

[_0_16_centos test]$ sudo killall -u harry
[_0_16_centos test]$ ps aux | grep harry
alvin    14040  0.0  0.0 112648   964 pts/0    R+   14:58   0:00 grep --color=auto harry

但是,这个选项要慎用,因为它会把该用户所有进程,包括终端进程,全部杀死,将导致该用户直接退出。所以,如果不想挨揍的话不要轻意尝试这个选项。

3. 终于时间的方式终止进程

假如我们现在运行了很多程序,我们只想杀死运行时间超过 5h 的进程,那么可以使用 -o 选项,其中 o 代表 older 如下:

killall -o 5h

同样地,如果你想杀死进行时间小于 4h 的进程,那么可以使用 -y 选项,其中 y 代表 younger ,如下:

killall -y 4h

这两个选项同样非常粗暴,也会把终端退出,所以先不演示了。

4. 忽略大小写

默认情况下,killall 命令是大小写敏感的,所以我们如果写错大小写,将无法正确杀死进程。

[_0_16_centos test]$ killall HELLO1
TEST1: no process found

如果我们想忽略大小写,可以加上 -I (大写字母 i )选项。

[_0_16_centos test]$ killall -I HELLO1
[1]   Terminated              ./hello1

5. 关闭命令执行回显

默认情况下,killall 会告诉你命令执行情况,但是,我们如果不关心它的执行结果,只想让它静默执行,该怎么办?只需加上 -q 选项即可,其中 q 表示 quite , 如下:

[_0_16_centos test]$ killall HELLO2
HELLO2: no process found
[_0_16_centos test]$ killall -q HELLO2
[_0_16_centos test]$

6. 列出所有支持的信号

如前文所述,默认情况下,killall 命令将发送 SIGTERM 信号,那么,安可以发送其它信号吗?当然是可以的。可以使用 -l 选项查看 killall 所支持的所有信号:

[_0_16_centos test]$ 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

你可以使用 -s 选项(后面跟一个信号名)来向一个进程发送特殊信号。

7. 交互式操作

如果你在杀死多个进程时不太放心,担心把不该杀死的进程给杀死了,那么你可以使用 -I 选项,这样就可以自由决定哪些进程应该被杀死,哪些进程应该被保留。

[_0_16_centos test]$ killall -i hello*
Kill hello2(13825) ? (y/N) y
Kill hello3(13831) ? (y/N) N
hello: no process found
hello1: no process found
hello3: no process found
hello.c: no process found
[2]-  Terminated              ./hello2

8. 等待直到某个进程被终止

当一个信号被发送至某个进程,如果你想确定该进程已经被杀死了才返回执行结果,可以使用 -w 选项,其中 w 代表 wait ,如下:

[_0_16_centos test]$ killall -w hello1
[4]+  Terminated              ./hello1

这里好像看不出什么效果,但实际执行的时候,可以发现执行结果会在一两秒后出现,而不加 -w 选项的话,执行结果马上就显示。


公众号:良许Linux

Linux 下使用 killall 命令终止进程的 8 大用法

有收获?希望老铁们来个三连击,给更多的人看到这篇文章

相关推荐