Linux Learn 28-linux One Line Command Kills Processes with Specified Names (killall, kill, pkill)

Preface

Common interview questions: How to use a linux directive to find a process with a specified name and kill it
Three commands commonly used to kill processes: killall, kill, pkill

Several ways to kill a process

The killall command is used to kill processes by name.

killall [parameter] [process name]

The kill Command kills the process of the specified process PID

kill [parameter] [process id]

pkill is similar to killall and is a process used to kill a specified name

pkill [parameter] [process name]

killall

Use ps first to find out what processes need to be killed

Ps-ef | grep process name

For example, I want to kill httpd processes

[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

There are so many processes found that if you want to kill them all at once, you can use killall

killall -9 httpd

Going back to see the process was killed

[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 and killall are similar in usage and kill all according to the process name

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 is used to kill a single process. Depending on the PID of the process, the second column found by ps-ef is the 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

Here kill is to kill a single process PID, if you want to kill all, you need to cooperate with awk command

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]# 

Parameter Description

  • Ps-ef is the command to view all processes in linux.
  • grep httpd All processes with the keyword "httpd"
  • Awk'{print $2}'outputs PID for column 2
  • xargs is the conversion of multiple PID standard inputs that are obtained into command line parameters.
  • Kill-9 kills the corresponding PID obtained by xargs

If you know the process name exactly, you can use killall and pkill directly. If you match the process name ambiguously, use the last one

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

Keywords: Apache Linux

Added by dcace on Thu, 30 Apr 2020 03:14:06 +0300