I want to kill all processes that result from the following command

5,888

Solution 1

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.

Solution 2

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'
Share:
5,888
I AM L
Author by

I AM L

Updated on September 18, 2022

Comments

  • I AM L
    I AM L over 1 year

    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?

    • Mathias Begert
      Mathias Begert over 11 years
      have you looked at pkill?
    • Kotte
      Kotte over 11 years
      Couldn't you use kill $(pidof vmstat) or pidof vmstat | xargs kill?
    • I AM L
      I AM L over 11 years
      @Kotte I don't seem to have the 'pidof' command, as I'm currently running this on a Solaris box.
    • Kevdog777
      Kevdog777 about 8 years
      Can $ killall vmstat not do it for you?
  • Richard Fortune
    Richard Fortune over 11 years
    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.
  • otokan
    otokan over 11 years
    You can (and should) get rid of grep in this case: ps -ef | awk '/vmstat 1/ { print "kill " $2 }' | bash
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 11 years
    Just use pkill… if you have it! I AM L may not be running Linux.
  • Stéphane Chazelas
    Stéphane Chazelas over 11 years
    @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.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 11 years
    “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.
  • Razzlero
    Razzlero over 11 years
    The "!/grep/" gets rid of grep.
  • otokan
    otokan over 11 years
    You don't have to use grep command at all in this case.
  • Jeff Schaller
    Jeff Schaller about 8 years
    Looks like the stackexchange formatter turned your backticks into code, making for a misleading answer. You might have intended: kill -9 $(pgrep -f vmstat)