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

  • 3
    Did you try pkill? Alternatively, you may want to use xargs. Commented Jul 15, 2016 at 10:07
  • 1
    Note that man pages are also good for stuff like this. Often they not only tell you how to use the command, they also tell you about related commands. In this case, the pgrep man page mentions pkill all over the place (as the 2 commands come from the same package).
    – phemmer
    Commented Jul 15, 2016 at 12:37

5 Answers 5


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
  • 4
    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.
    – Kusalananda
    Commented Jul 15, 2016 at 10:37
  • 3
    @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.
    – hobbs
    Commented Jul 15, 2016 at 19:33
  • 1
    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. Commented Jul 15, 2016 at 23:01
  • 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).
    – Joe
    Commented Jul 17, 2016 at 8:11
  • 1
    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.
    – Barmar
    Commented Jul 20, 2016 at 17:35

You can use killall as well, e.g.

killall firefox

to send SIGTERM to all firefox processes.

  • 8
    Note that killall has different meanings on different unix systems - if you're on a non-Linux system make sure to check the documentation.
    – Random832
    Commented Jul 15, 2016 at 14:54
  • killall -KILL firefox can feel sooo rewarding.
    – pipe
    Commented Jul 16, 2016 at 10:20
  • The non-portability of killall is why I never use it. Running killall on a solaris box for example is disastrous.
    – phemmer
    Commented Jul 21, 2016 at 3:10

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

$ for proc in $(pgrep <process command>); do kill $proc; done
  • 1
    I get kill <no>:failed: operation not permitted
    – Timo
    Commented Nov 18, 2017 at 21:55
  • Probably you do not have the right permissions or the process does not exists anymore, please refer to: superuser.com/questions/1175485/…
    – lcipriani
    Commented Dec 11, 2017 at 13:23

You can kill multiple process with array. In this case you can specify options as $ip, $hostname or something similar.

ip=your_ip_address; declare -a arr=$(ps aux|grep tail|grep $ip|awk '{print $2}'); for pid in ${arr[@]}; do kill -9 $pid; done;

When you are running scripts, especially in a stacking way, giving process name does not work for me. Thus I am using this simple command:

ps -eaf | grep -i script_hint (or whatever condition is) | awk '{print $2}' | xargs kill -15

You must log in to answer this question.

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