4

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)?

4 Answers 4

5

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".

3

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".

1
  • I needed to use $2 on my Windows 10 bash Feb 11, 2017 at 16:26
2

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

2

pkill and killall are variants of this.

# pkill vim

# killall vim

Both has extended documentation in their respective man-pages.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .