2

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?

4
  • 4
    have you looked at pkill?
    – iruvar
    Dec 5, 2012 at 1:18
  • Couldn't you use kill $(pidof vmstat) or pidof vmstat | xargs kill?
    – Kotte
    Dec 5, 2012 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, 2012 at 5:53
  • Can $ killall vmstat not do it for you?
    – Kevdog777
    Apr 28, 2016 at 10:58

4 Answers 4

4

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.

1
  • 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, 2012 at 1:48
4

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 -Ao pid,args | awk '$2 == "vmstat" && $3 == "1" {print $1}' | xargs kill

Or

pkill -xf 'vmstat 1'
4
  • Just use pkill… if you have it! I AM L may not be running Linux. Dec 5, 2012 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. Dec 5, 2012 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. Dec 5, 2012 at 23:41
  • pkill actually isn't a bad idea either, tried it and it worked like a charm.
    – I AM L
    Dec 6, 2012 at 5:56
0

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.

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

As simple as:

$ kill -9 $(pgrep -f vmstat)
1
  • Looks like the stackexchange formatter turned your backticks into code, making for a misleading answer. You might have intended: kill -9 $(pgrep -f vmstat)
    – Jeff Schaller
    Apr 28, 2016 at 11:19

You must log in to answer this question.

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