一个比较好用的杀进程的命令


常用的杀进程的命令有kill killall,今天介绍一下pkill
pkill通常和killall一样,用于杀死一个相同进程名的多个进程,如:

localhost ~ # pidof test2
3693 3379 3071
localhost ~ # pkill test2
localhost ~ # pidof test2
localhost ~ # 

如果pkill的功能也仅仅是这样,它和killall就没有什么区别了。pkill强大的地方在于,它可以杀死一个用户的所有进程。

在一个终端,通过test用户登录,并开启了三个进程:

localhost /data/test $ whoami
test
localhost /data/test $ ./test2 &
[1] 3982
localhost /data/test $ ./test2 &
[2] 4330
localhost /data/test $ ./test2 &
[3] 4454
localhost /data/test $ pidof test2
4454 4330 3982

然后在另一个终端,通过root用户登录并查看test用户的所有进程如下:

localhost /data/test # whoami
root
localhost /data/test # ps aux | grep test
test       3982 91.1  0.0   3780   300 pts/3    R    10:42   0:23 ./test2
test       4330 48.2  0.0   3780   296 pts/3    R    10:42   0:12 ./test2
test       4454 47.2  0.0   3780   300 pts/3    R    10:42   0:11 ./test2
root      7886  0.0  0.0   6280   580 pts/1    S+   10:43   0:00 grep --colour=auto test
root     13974  0.0  0.1  65208  2880 ?        Ss   10:41   0:00 sshd: test [priv] 
test      14661  0.0  0.0  65208  1728 ?        S    10:41   0:00 sshd: test@pts/3  
test      14662  0.0  0.1  17796  1936 pts/3    Ss+  10:41   0:00 -bash

可以看到test用户总共有5个进程,另外的两个是sshd登录和bash,接下来使用root用户执行一下pkill命令:

localhost /data/test # pkill -u test
localhost /data/test # ps aux | grep test
root     28571  0.0  0.0   6280   580 pts/1    S+   10:45   0:00 grep --colour=auto test
localhost /data/test # 

一下子世界清静了,系统中再没有test用户的进程了。

pkill可以用于杀某个用户的所有进程,能否杀同一个组内所有用户的进程呢 ?
通过man文档得之,答案是肯定的!不仅如此,它还可以有许多的选项:

NAME
       pgrep, pkill - look up or signal processes based on name and other attributes


SYNOPSIS
       pgrep [-flvx] [-d delimiter] [-n|-o] [-P ppid,...] [-g pgrp,...]
            [-s sid,...] [-u euid,...] [-U uid,...] [-G gid,...]
            [-t term,...] [pattern]

       pkill [-signal] [-fvx] [-n|-o] [-P ppid,...] [-g pgrp,...]
            [-s sid,...] [-u euid,...] [-U uid,...] [-G gid,...]
            [-t term,...] [pattern]

下面就演示一下杀一个组的所有用户的所有进程吧。
先用两个终端分别用两个同属于test组的用户test、test2登录,然后分别开几个进程:


test@localhost ~ $ whoami
test
test@localhost ~ $ ./testprog &
[1] 31474
test@localhost ~ $ ./testprog &
[2] 31475
test@localhost ~ $ ./testprog &
[3] 31476

test2@localhost ~ $ whoami
test2
test2@localhost ~ $ ./testprog2 &
[1] 31485
test2@localhost ~ $ ./testprog2 &
[2] 31486
test2@localhost ~ $ ./testprog2 &
[3] 31487

然后用root用户使用pkill杀test用户组的所有进程:

~ # whoami
root
~ # ps aux  | grep test
root     31462  0.0  0.3  65152  2832 ?        Ss   11:18   0:00 sshd: test [priv]
test     31464  0.0  0.2  65152  1720 ?        S    11:18   0:00 sshd: test@pts/7 
test     31465  0.0  0.2  17736  1828 pts/7    Ss+  11:18   0:00 -bash
test     31474 99.7  0.0   3732   292 pts/7    R    11:18   6:14 ./testprog
test     31475 99.8  0.0   3732   296 pts/7    R    11:18   6:13 ./testprog
test     31476 99.7  0.0   3732   296 pts/7    R    11:18   6:12 ./testprog
root     31477  0.0  0.3  65152  2832 ?        Ss   11:18   0:00 sshd: test2 [priv]
test2    31479  0.0  0.2  65152  1720 ?        S    11:18   0:00 sshd: test2@pts/8
test2    31480  0.0  0.2  17736  1800 pts/8    Ss+  11:18   0:00 -bash
test2    31485 99.9  0.0   3732   296 pts/8    R    11:18   5:55 ./testprog2
test2    31486 99.7  0.0   3732   292 pts/8    R    11:18   5:55 ./testprog2
test2    31487 99.7  0.0   3732   296 pts/8    R    11:18   5:54 ./testprog2
root     31549  0.0  0.0   6228   576 pts/6    S+   11:24   0:00 grep --colour=auto test
~ # pkill -G test
~ # ps aux  | grep test
root     31554  0.0  0.0   6228   576 pts/6    S+   11:24   0:00 grep --colour=auto test

原来要灭一个组的也这么简单。

类似pkill的命令,还有xkill命令用于杀图形界面进程。由于我用的系统没有图形界面,也没有xkill命令,留待以后尝试使用吧。

总结:
1. linux中杀进程的命令有 kill, killall, pkill, xkill
2. pkill可以用于杀一个用户或一个组所有的进程,当然还有其他的一些强大的功能选项。
3. xkill可以用于杀图形进程
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值