Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

The following command will display all the PID's running for vmstat1:

ps -ef | grep "vmstat 1" | awk '{ print $2 }'

My question is, how do I kill them all, if there's like 20 of them at once?

share|improve this question
4  
have you looked at pkill? –  1_CR Dec 5 '12 at 1:18
    
Couldn't you use kill $(pidof vmstat) or pidof vmstat | xargs kill? –  Kotte Dec 5 '12 at 10:06
    
@Kotte I don't seem to have the 'pidof' command, as I'm currently running this on a Solaris box. –  I AM L Dec 6 '12 at 5:53

3 Answers 3

up vote 2 down vote accepted

If your command produces list of PIDs, then simply pipe it into:

xargs kill

Note that your command will match the grep command as well, so consider adding something like |grep -v grep before the original grep command.

share|improve this answer
    
Another idiom to avoid having the grep itself in the list is to use "grep '[f]oo'" instead of "grep 'foo'". Can't type backquotes on this android keypad, annoyingly. –  dubiousjim Dec 5 '12 at 1:48

Just use pkill. Though not a standard command, it is found in many Unices and is dedicated to this kind of tasks.

pkill -f 'vmstat 1'

Also note that your grep will match vmstat 1 but also vmstat 10 and grep vmstat 1 (so would that pkill above), and awk is a superset of grep. To be more robust, you could do instead:

ps -eo pid,args | awk '$2 == "vmstat" && $3 == "1" {print $1}' | xargs kill

Or

pkill -xf 'vmstat 1'
share|improve this answer
    
Just use pkill… if you have it! I AM L may not be running Linux. –  Gilles Dec 5 '12 at 23:11
    
@Gilles. pkill is not a Linux invention. I believe it originated in Solaris and it is also found at least in FreeBSD, NetBSD. But it's true it's not a standard command, I'll add the note. –  Stéphane Chazelas Dec 5 '12 at 23:28
    
“The pkill and pgrep utilities first appeared in NetBSD 1.6. They are modelled after utilities of the same name that appeared in Sun Solaris 7. They made their first appearance in FreeBSD 5.3.” Also OpenBSD 3.5. Quickly made its way into Linux. Not de facto standard because it is missing from OSX. –  Gilles Dec 5 '12 at 23:41
    
pkill actually isn't a bad idea either, tried it and it worked like a charm. –  I AM L Dec 6 '12 at 5:56

If you are already using awk, you can do

ps -ef | grep "vmstat 1" | awk '!/grep/{ print "kill " $2 }' | bash

It will output a list of kill commands, and as every one of the commands kills a single process, you will not run into potential argument list too long (though it's highly unlikely when going over running processes), and I added a pattern to avoid matching the grep process itself, as killing it may cut your list short.

share|improve this answer
    
You can (and should) get rid of grep in this case: ps -ef | awk '/vmstat 1/ { print "kill " $2 }' | bash –  otokan Dec 5 '12 at 19:42
    
The "!/grep/" gets rid of grep. –  David Kohen Dec 6 '12 at 6:10
    
You don't have to use grep command at all in this case. –  otokan Dec 6 '12 at 8:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.