| 
                        副标题[/!--empirenews.page--]
                           
Linux 的命令行提供很多命令来杀死进程。比如,你可以向 kill 命传递一个PID来杀死进程;pkill 命令使用一个正则表达式作为输入,所以和该模式匹配的进程都被杀死。 
但是还有一个命令叫 killall ,默认情况下,它精确地匹配参数名,然后杀死匹配进程。在这篇文章中,我们将讨论有关这个命令的实际应用。 
默认情况下,killall 命令将向一个/组进程发送一个 SIGTERM 信号,但是,也可以通过参数发送一个指定的信号。 
下面我们通过例子详细介绍 killall 的 8 大用法。 
1、基本用法 
假如我们 3 个进程在运行,分别是 hello1, hello2, hello3 ,现在我们想杀死 hello1 进程,可以直接使用如下方式: 
- killall hello1 
 
  
运行的结果如下: 
- [alvin@VM_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  
 - [alvin@VM_0_16_centos test]$ killall hello1  
 - [1]   Terminated              ./hello1  
 - [alvin@VM_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 进程,我们想一次性杀死他们,也就是批量杀死进程,可以如下操作: 
- [alvin@VM_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 现在运行如下几个进程: 
- [alvin@VM_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: 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 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 
 
  
                                                (编辑:52站长网) 
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! 
                     |