How to kill multiple processes

76,546

Solution 1

You can use pkill:

pkill httpd

You may also want to use process substitution(although this isn't as clear):

kill $(pgrep command)

And you may want to use xargs:

pgrep command | xargs kill

Solution 2

You can use killall as well, e.g.

killall firefox

to send SIGTERM to all firefox processes.

Solution 3

Yes, you can use a bash feature and looping over the output.

$ for proc in $(pgrep <process command>); do kill $proc; done
Share:
76,546

Related videos on Youtube

blue-sky
Author by

blue-sky

Updated on September 18, 2022

Comments

  • blue-sky
    blue-sky over 1 year

    to find the PID of the process to kill use :

    pgrep <process command>
    

    I then use the kill command to kill the PID returned by pgrep <process command>

    kill <PID>
    

    Can these commands be combined into one so can kill the PID or PID's returned by pgrep <process command> ? Or is there a method kill multiple processes by command name ?

    Something like : kill(pgrep <name of process>)

  • Kusalananda
    Kusalananda almost 8 years
    I did a similar thing in an answer and it was pointed out to me that I introduced a race condition. The process IDs may be invalidated inbetween the calls to pgrep and kill. Just use pkill.
  • Random832
    Random832 almost 8 years
    Note that killall has different meanings on different unix systems - if you're on a non-Linux system make sure to check the documentation.
  • hobbs
    hobbs almost 8 years
    @Kusalananda the same thing can also happen between pkill getting the process list and actually sending the signal, it's just harder to see in that case.
  • Ross Presser
    Ross Presser almost 8 years
    I was going to try to compose an answer that looped something like kill $(ps|head -1) to avoid the race condition ... but there really isn't a way to avoid it. The process could die at any point in the pipeline.
  • pipe
    pipe almost 8 years
    killall -KILL firefox can feel sooo rewarding.
  • gokhan acar
    gokhan acar almost 8 years
    Just be careful with pkill because some programs may have more than one instance running and you might not want to kill all of them. Running pgrep first will help as long as another one doesn't start between the time you run pgrep and pkill (race).
  • Barmar
    Barmar almost 8 years
    Even a C program can't avoid the race condition, the window will just be smaller. The only way to do it truly atomically would be to add a system call that kills processes by name. But process IDs generally won't be reused very quickly, so the danger is virtually nonexistent.
  • phemmer
    phemmer almost 8 years
    The non-portability of killall is why I never use it. Running killall on a solaris box for example is disastrous.
  • Timo
    Timo over 6 years
    I get kill <no>:failed: operation not permitted
  • Lohrun
    Lohrun over 6 years
    Probably you do not have the right permissions or the process does not exists anymore, please refer to: superuser.com/questions/1175485/…