20080918 kill all processes that match a pattern - plembo/onemoretech GitHub Wiki

title: kill all processes that match a pattern link: https://onemoretech.wordpress.com/2008/09/18/kill-all-processes-that-match-a-pattern/ author: lembobro description: post_id: 455 created: 2008/09/18 00:45:03 created_gmt: 2008/09/18 00:45:03 comment_status: open post_name: kill-all-processes-that-match-a-pattern status: publish post_type: post

kill all processes that match a pattern

(post updated in 2012) Just had to do this to shut down a bunch of errant processes on an Apache web server after "/etc/init.d/httpd stop” failed. So here’s what you’d get from a “ps -ef”:

[me@bigserver1121 ~]$ ps -ef | grep httpd
root     10193     1  0 10:27 ?        00:00:00 /usr/sbin/httpd
apache   10195 10193  0 10:27 ?        00:00:00 /usr/sbin/httpd
apache   10196 10193  0 10:27 ?        00:00:00 /usr/sbin/httpd
apache   10197 10193  0 10:27 ?        00:00:00 /usr/sbin/httpd
apache   10198 10193  0 10:27 ?        00:00:00 /usr/sbin/httpd
apache   10199 10193  0 10:27 ?        00:00:00 /usr/sbin/httpd
apache   10200 10193  0 10:27 ?        00:00:00 /usr/sbin/httpd

To stop every process associated with this instance of Apache, I issued this one liner at the web server console:

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

Basically what this does is go out and search for all the processes that match the pattern “httpd”, pulls out the 2nd column of the output and pipes this into xargs for it to pass on to the kill command. Given this uses such basic utilities, it should work on any Unix-like system. Note: For some applications, especially where there are multiple instances with the same process name, you might want to get more targeted. For example, if I had 2 httpd processes, one running out of /usr/sbin/httpd and another /opt/apache/sbin/httpd, then I might use something like the following to kill the processes belonging to the former:

ps -ef | grep '/usr/sbin/httpd' | grep -v grep | awk '{print $2}' | xargs kill

Copyright 2004-2019 Phil Lembo