第四节Linux下关闭所有终端的方法 (shell)

一.killall使用方法(结束大多数进程)

参考:http://www.cnblogs.com/peida/archive/2012/12/21/2827366.html

1.命令格式:

killall[参数][进程名]

2.命令功能:
用来结束同名的的所有进程
3.例子:
实例1:杀死所有同名进程
命令:

killall vi

输出:

[root@localhost ~]# ps -ef|grep vi
 
root     17581 17398  0 17:51 pts/0    00:00:00 vi test.txt
 
root     17611 17582  0 17:51 pts/1    00:00:00 grep vi
 
[root@localhost ~]# ps -ef|grep vi
 
root     17581 17398  0 17:51 pts/0    00:00:00 vi test.txt
 
root     17640 17612  0 17:51 pts/2    00:00:00 vi test.log
 
root     17642 17582  0 17:51 pts/1    00:00:00 grep vi
 
[root@localhost ~]# killall vi
 
[root@localhost ~]# ps -ef|grep vi
 
root     17645 17582  0 17:52 pts/1    00:00:00 grep vi

实例3:把所有的登录后的shell给杀掉
命令:

killall -9 bash

二.kill使用方法(结束特定进程)

参考:http://blog.csdn.net/andy572633/article/details/7211546

1.查看进程
命令:ps -ef 或者 ps -aux

czz@czz:~$ ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 09:35 ?        00:00:02 /lib/systemd/systemd --system --
root         2     0  0 09:35 ?        00:00:00 [kthreadd]
root         4     2  0 09:35 ?        00:00:00 [kworker/0:0H]
root         6     2  0 09:35 ?        00:00:00 [mm_percpu_wq]
czz       9908  1646 36 15:24 ?        00:00:06 /usr/lib/firefox/firefox

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

$ kill -s 9 9908

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

$ ps -ef | grep firefox
czz@czz:~$ ps -ef |grep firefox
czz       9908  1646  7 15:24 ?        00:00:07 /usr/lib/firefox/firefox
czz       9972  9908  0 15:24 ?        00:00:00 /usr/lib/firefox/firefox -contentproc -childID 1 -isForBrowser -prefsLen 1 -prefMapSize 209321 -parentBuildID 20200107212822 -greomni /usr/lib/firefox/omni.ja -appomni /usr/lib/firefox/browser/omni.ja -appdir /usr/lib/firefox/browser 9908 true tab
czz       9994  9908  5 15:24 ?        00:00:05 /usr/lib/firefox/firefox -contentproc -childID 2 -isForBrowser -prefsLen 1 -prefMapSize 209321 -parentBuildID 20200107212822 -greomni /usr/lib/firefox/omni.ja -appomni /usr/lib/firefox/browser/omni.ja -appdir /usr/lib/firefox/browser 9908 true tab
czz      10081  9908  0 15:24 ?        00:00:00 /usr/lib/firefox/firefox -contentproc -childID 3 -isForBrowser -prefsLen 6950 -prefMapSize 209321 -parentBuildID 20200107212822 -greomni /usr/lib/firefox/omni.ja -appomni /usr/lib/firefox/browser/omni.ja -appdir /usr/lib/firefox/browser 9908 true tab
czz      10193 10122  0 15:26 pts/21   00:00:00 grep --color=auto firefox

关闭特定火狐进程

$kill -s 9 9908

3.方法二:pgrep firefox

czz@czz:~$ pgrep firefox
9908
$kill -s 9 9908

4.方法三:使用pidof

$ pidof firefox-bin
9908

PS:和pgrep相比稍显不足的是,pidof必须给出进程的全名

三.改进

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

说明:
“grep firefox”的输出结果是,所有含有关键字“firefox”的进程。
“grep -v grep”是在列出的进程中去除含有关键字“grep”的进程。
“cut -c 9-15”是截取输入行的第9个字符到第15个字符,而这正好是进程号PID。
“xargs kill -s 9”中的xargs命令是用来把前面命令的输出结果(PID)作为“kill -s 9”命令的参数,并执行该命令。“kill -s 9”会强行杀掉指定进程

知道pgrep和pidof两个命令,干嘛还要打那么长一串!

$ pgrep firefox | xargs kill -s 9
$ ps -ef | grep firefox | awk '{print $2}' | xargs kill -9
kill: No such process

其中awk '{print $2}' 的作用就是打印(print)出第二列的内容。根据常规篇,可以知道ps输出的第二列正好是PID。就把进程相应的PID通过xargs传递给kill作参数,杀掉对应的进程。

难道每次都要调用xargs把PID传递给kill?答案是否定的:

$kill -s 9 `ps -aux | grep firefox | awk '{print $2}'`

没错,命令依然有点长,换成pgrep

$kill -s 9 `pgrep firefox`

6.pkill:
pkill=pgrep+kill

$pkill -9 firefox

7.killall:
killall和pkill是相似的,不过如果给出的进程名不完整,killall会报错。pkill或者pgrep只要给出进程名的一部分就可以终止进程。

$killall -9 firefox

四.

如果有多条执行语句,在gnome-terminal中要加双引号,如果只有一条的话可以直接执行,不用加引号
例如:

gnome-terminal -t "clear1" -x bash -c "ps -ef | grep 
rplidar_amcl_demo.launch | cut -c 9-15 | xargs kill -s 9 ; exec bash";

你可能感兴趣的:(第四节Linux下关闭所有终端的方法 (shell))