这篇文章给大家介绍怎么在Linux中使用killall命令终止进程,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
<强> 1,基本用法强>
假如我们3个进程在运行,分别是hello1, hello2, hello3,现在我们想杀死hello1进程,可以直接使用如下方式:
<代码> killall hello1 代码>
运行的结果如下:
[alvin@VM_0_16_centos 测试],美元ps aux |, grep hello alvin 0.0, 12061,, 0.0,, 4152,, 344,分/0,,S , 14:41 , 0:00 。/hello1 alvin 0.0, 12074,, 0.0,, 4152,, 344,分/0,,S , 14:41 , 0:00 。/hello2 alvin 0.0, 12084,, 0.0,, 4152,, 340,分/0,,S , 14:41 , 0:00 。/hello3 alvin , 12089, 0.0, 0.0, 112648,, 964,分/0,,R +,, 14:41 , 0:00 grep ——颜色=auto hello (alvin@VM_0_16_centos 测试),美元killall hello1 [1],,Terminated ,,,,,,。/hello1 (alvin@VM_0_16_centos 测试),美元ps aux |, grep hello alvin 0.0, 12074,, 0.0,, 4152,, 344,分/0,,S , 14:41 , 0:00 。/hello2 alvin 0.0, 12084,, 0.0,, 4152,, 340,分/0,,S , 14:41 , 0:00 。/hello3 alvin , 12170, 0.0, 0.0, 112648,, 964,分/0,,R +,, 14:42 , 0:00 grep ——颜色=auto 你好
可以看的到,hello1进程已经被杀死了。
剩下的hello2和hello3进程,我们想一次性杀死他们,也就是批量杀死进程,可以如下操作:
[alvin@VM_0_16_centos 测试],美元killall 你好*, 你好:no process  found hello1: no process  found 安全:no process  found [2]的背后,Terminated ,,,,,,。/hello2 [3]+,Terminated ,,,,,。/hello3
如此,以你好开头的进程全部被干掉。
<强> 2,终止某个用户所运行的进程强>
我们可以杀死以满足某个正则表达式的一组进程,同样的,我们也可以杀死某个用户运行的所有进程。
比如,用户哈里现在运行如下几个进程:
[alvin@VM_0_16_centos 测试],美元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:, harry@pts/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@notty 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,分/1,,Ss +, 14:55 , 0:00 -bash harry 0.0, 13948,, 0.0,, 4152,, 344,分/1,,S , 14:57 , 0:00 。/hello1 harry 0.0, 13952,, 0.0,, 4152,, 344,分/1,,S , 14:57 , 0:00 。/hello2 harry 0.0, 13959,, 0.0,, 4152,, 344,分/1,,S , 14:57 , 0:00 。/hello3 alvin , 14005, 0.0, 0.0, 112648,, 964,分/0,,R +,, 14:58 , 0:00 grep ——颜色=auto 哈利
我们现在想杀哈利死所运行的所有进程,可以以如下方式操作:
<代码> killall哈里- u 代码>
运行结果如下:
[alvin@VM_0_16_centos 测试],美元sudo killall -u harry (alvin@VM_0_16_centos 测试),美元ps aux |, grep harry alvin , 14040, 0.0, 0.0, 112648,, 964,分/0,,R +,, 14:58 , 0:00 grep ——颜色=auto 哈利
但是,这个选项要慎用,因为它会把该用户所有进程,包括终端进程,全部杀死,将导致该用户直接退出,所以,如果不想挨揍的话不要轻意尝试这个选项。
<强> 3,终于时间的方式终止进程强>
假如我们现在运行了很多程序,我们只想杀死运行时间超过5 h的进程,那么可以使用- o选项,其中o代表老如下:
<代码> killall - o 5 h 代码>
同样地,如果你想杀死进行时间小于4 h的进程,那么可以使用- y选项,其中y代表,如下:
<代码> killall - y 4 h 代码>
这两个选项同样非常粗暴,也会把终端退出,所以先不演示了。
<强> 4,忽略大小写强>
默认情况下,killall命令是大小写敏感的,所以我们如果写错大小写,将无法正确杀死进程。
[alvin@VM_0_16_centos 测试],美元killall HELLO1 null null null null null null null null null null null null null null null null怎么在Linux中使用killall命令终止进程