系统相关
首页 > 系统相关> > 【Linux命令】linux一行命令杀掉指定名称进程(killall 、kill 、pkill)

【Linux命令】linux一行命令杀掉指定名称进程(killall 、kill 、pkill)

作者:互联网

前言

常见面试题:如何用一句 linux 指令,查找到指定名称的进程并杀掉
常用的杀进程的三个指令:killall 、kill 、pkill

杀进程几种方式

killall命令用于杀死指定名字的进程(kill processes by name)

killall [参数] [进程名]

kill命令杀死指定进程PID的进程

kill [参数] [进程id]

pkill 和killall差不多,也是用于杀掉指定名称的进程

pkill [参数] [进程名]

killall

先用ps查找出需要杀掉的进程

ps -ef |grep 进程名称

比如我想杀掉 httpd 的进程

[root@VM_0_2_centos opt]# ps -ef | grep httpd
root     21982 21885  0 21:06 ?        00:00:00 /opt/zbox/run/apache/httpd -k start
65534    21983 21982  0 21:06 ?        00:00:00 /opt/zbox/run/apache/httpd -k start
65534    21984 21982  0 21:06 ?        00:00:00 /opt/zbox/run/apache/httpd -k start
65534    21999 21982  0 21:06 ?        00:00:00 /opt/zbox/run/apache/httpd -k start
root     22321 19229  0 21:06 pts/0    00:00:00 grep --color=auto httpd 

查找出来的进程很多,如果想一次性杀掉,可以用killall

killall -9 httpd

再去查看进程就被杀掉了

[root@VM_0_2_centos opt]# killall -9 httpd
[root@VM_0_2_centos opt]# ps -ef | grep httpd
root     22781 19229  0 21:08 pts/0    00:00:00 grep --color=auto httpd 

pkill

pkill 和 killall用法差不多,也是根据进程名称来杀掉全部

pkill -9 httpd

[root@VM_0_2_centos opt]# ps -ef | grep httpd
root     23284 23216  0 21:09 ?        00:00:00 /opt/zbox/run/apache/httpd -k start
65534    23290 23284  0 21:09 ?        00:00:00 /opt/zbox/run/apache/httpd -k start
65534    23292 23284  0 21:09 ?        00:00:00 /opt/zbox/run/apache/httpd -k start
65534    23294 23284  3 21:09 ?        00:00:00 /opt/zbox/run/apache/httpd -k start
root     23377 19229  0 21:09 pts/0    00:00:00 grep --color=auto httpd
[root@VM_0_2_centos opt]# pkill -9 httpd
[root@VM_0_2_centos opt]# ps -ef | grep httpd
root     23680 19229  0 21:09 pts/0    00:00:00 grep --color=auto httpd 

kill

kill 是用于杀掉单个进程,根据进程的PID, ps -ef查出来的第二列就是PID

[root@VM_0_2_centos opt]# ps -ef | grep httpd
root     24431 24361  0 21:11 ?        00:00:00 /opt/zbox/run/apache/httpd -k start
65534    24432 24431  0 21:11 ?        00:00:00 /opt/zbox/run/apache/httpd -k start
65534    24433 24431  0 21:11 ?        00:00:00 /opt/zbox/run/apache/httpd -k start
65534    24445 24431  0 21:11 ?        00:00:00 /opt/zbox/run/apache/httpd -k start
root     24519 19229  0 21:11 pts/0    00:00:00 grep --color=auto httpd
[root@VM_0_2_centos opt]# kill -9 24431 

这里 kill 是杀掉单个进程PID,如果想杀掉全部的,需配合 awk 命令

ps -ef | grep httpd | awk '{print $2}' | xargs kill -9

[root@VM_0_2_centos opt]# ps -ef | grep httpd
root     26267 26194  0 21:15 ?        00:00:00 /opt/zbox/run/apache/httpd -k start
65534    26269 26267  0 21:15 ?        00:00:00 /opt/zbox/run/apache/httpd -k start
65534    26270 26267  0 21:15 ?        00:00:00 /opt/zbox/run/apache/httpd -k start
65534    26273 26267  0 21:15 ?        00:00:00 /opt/zbox/run/apache/httpd -k start
root     26358 19229  0 21:15 pts/0    00:00:00 grep --color=auto httpd
[root@VM_0_2_centos opt]# ps -ef | grep httpd | awk '{print $2}' | xargs kill -9
kill: sending signal to 26652 failed: No such process
[root@VM_0_2_centos opt]# ps -ef | grep httpd
root     26664 19229  0 21:15 pts/0    00:00:00 grep --color=auto httpd
[root@VM_0_2_centos opt]# 

参数说明

如果能准确知道进程名称,可以直接用killall 和pkill ,如果是模糊匹配进程名称,就用最后一个

ps -ef | grep 进程名称 | awk '{print $2}' | xargs kill -9

标签:opt,httpd,00,grep,pkill,命令,Linux,root,21
来源: https://www.cnblogs.com/wchw2008/p/15594434.html