Is there a Unix-type command to kill all processes with a certain name?

I run a script called "delayed_job" for a Ruby on Rails application. One of the options is to run this proc with a separate monitor proc. When the main proc dies, the monitor will spawn a new one. I can also run multiple processes. Each will have its own associated monitor process.

The script gives a way to kill the main processes, but not the monitor processes. I want to kill all of them.

A command of:

ps -ef|grep delayed

yields:

42011    29423     1  0 Sep25 ?        00:00:02 delayed_job.0_monitor                
42011    29428     1  0 Sep25 ?        00:00:02 delayed_job.1_monitor                
42011    29434     1  0 Sep25 ?        00:00:02 delayed_job.2_monitor                
42011    29437     1  0 Sep25 ?        00:00:01 delayed_job.3_monitor                
42011    23359     1 10 Oct19 ?        03:12:49 delayed_job.0                        
42011     8607     1  5 Oct19 ?        00:58:42 delayed_job.3                        
42011    21442     1 44 12:31 ?        01:02:03 delayed_job.2                        
42011    23092     1  4 14:18 ?        00:01:22 delayed_job.1                        
42011    23861 23763  0 14:51 pts/5    00:00:00 grep delayed

I want to kill all of them, or at least the monitors. How can I kill these (with the exception of the grep command itself)?


Solution 1:

http://www.tech-recipes.com/rx/742/kill-the-undesired-unix-processes-in-one-go/ has a nice write up of the usual answer for generic unix: pipe ps to grep, then to awk, then to xargs.

As another poster mentioned, beware any glib answer that throws around "killall", since it has a radically different purpose on AIX and Solaris (at least) than it does on Linux. Running killall as root on Solaris is a "resume generating event".

Solution 2:

Outside of the above pkill and killall answers, you can do the following, assuming you want to kill process "foo"

ps -ef | grep [f]oo | awk {'print $1'} | xargs kill

The grep [f]oo means that grep will not match the grep command itself, so it will just kill processes named "foo".

Solution 3:

If you are running a linux machine, the killall command seems to be what you are looking for.

Solution 4:

pkill and killall are variants of this.

# pkill vim

# killall vim

Both has extended documentation in their respective man-pages.