Linux下强制杀死进程的方法详解

2019-10-09 16:18:11王冬梅

常规篇:

首先,用ps查看进程,方法如下:

$ ps -ef

……
smx 1822 1 0 11:38 ? 00:00:49 gnome-terminal
smx 1823 1822 0 11:38 ? 00:00:00 gnome-pty-helper
smx 1824 1822 0 11:38 pts/0 00:00:02 bash
smx 1827 1 4 11:38 ? 00:26:28 /usr/lib/firefox-3.6.18/firefox-bin
smx 1857 1822 0 11:38 pts/1 00:00:00 bash
smx 1880 1619 0 11:38 ? 00:00:00 update-notifier
……
smx 11946 1824 0 21:41 pts/0 00:00:00 ps -ef

或者:

$ ps -aux

……

smx 1822 0.1 0.8 58484 18152 ? Sl 11:38 0:49 gnome-terminal
smx 1823 0.0 0.0 1988 712 ? S 11:38 0:00 gnome-pty-helper
smx 1824 0.0 0.1 6820 3776 pts/0 Ss 11:38 0:02 bash
smx 1827 4.3 5.8 398196 119568 ? Sl 11:38 26:13 /usr/lib/firefox-3.6.18/firefox-bin
smx 1857 0.0 0.1 6688 3644 pts/1 Ss 11:38 0:00 bash
smx 1880 0.0 0.6 41536 12620 ? S 11:38 0:00 update-notifier
……
smx 11953 0.0 0.0 2716 1064 pts/0 R+ 21:42 0:00 ps -aux

此时如果我想杀了火狐的进程就在终端输入:

$ kill -s 9 1827

其中-s 9 制定了传递给进程的信号是9,即强制、尽快终止进程。各个终止信号及其作用见附录。

1827则是上面ps查到的火狐的PID。

简单吧,但有个问题,进程少了则无所谓,进程多了,就会觉得痛苦了,无论是ps -ef 还是ps -aux,每次都要在一大串进程信息里面查找到要杀的进程,看的眼都花了。

进阶篇:

改进1:

把ps的查询结果通过管道给grep查找包含特定字符串的进程。管道符“|”用来隔开两个命令,管道符左边命令的输出会作为管道符右边命令的输入。

$ ps -ef | grep firefox

smx 1827 1 4 11:38 ? 00:27:33 /usr/lib/firefox-3.6.18/firefox-bin
smx 12029 1824 0 21:54 pts/0 00:00:00 grep --color=auto firefox

这次就清爽了。然后就是

$kill -s 9 1827

改进2——使用pgrep:

一看到pgrep首先会想到什么?没错,grep!pgrep的p表明了这个命令是专门用于进程查询的grep。

$ pgrep firefox
1827

看到了什么?没错火狐的PID,接下来又要打字了:

$kill -s 9 1827

改进3——使用pidof:

看到pidof想到啥?没错pid of xx,字面翻译过来就是 xx的PID。

$ pidof firefox-bin

1827

和pgrep相比稍显不足的是,pidof必须给出进程的全名。然后就是老生常谈:

$kill -s 9 1827

无论使用ps 然后慢慢查找进程PID 还是用grep查找包含相应字符串的进程,亦或者用pgrep直接查找包含相应字符串的进程PID,然后手动输入给kill杀掉

改进4:

$ps -ef | grep firefox | grep -v grep | cut -c 9-15 | xargs kill -s 9

说明:

“grep firefox”的输出结果是,所有含有关键字“firefox”的进程。

“grep -v grep”是在列出的进程中去除含有关键字“grep”的进程。