29

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

2
  • 3
    Did you try pkill? Alternatively, you may want to use xargs. Jul 15 '16 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
    Jul 15 '16 at 12:37
35

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
5
  • 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
    Jul 15 '16 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
    Jul 15 '16 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. Jul 15 '16 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
    Jul 17 '16 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
    Jul 20 '16 at 17:35
18

You can use killall as well, e.g.

killall firefox

to send SIGTERM to all firefox processes.

3
  • 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
    Jul 15 '16 at 14:54
  • killall -KILL firefox can feel sooo rewarding.
    – pipe
    Jul 16 '16 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
    Jul 21 '16 at 3:10
11

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

$ for proc in $(pgrep <process command>); do kill $proc; done
2
  • I get kill <no>:failed: operation not permitted
    – Timo
    Nov 18 '17 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
    Dec 11 '17 at 13:23
0

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;
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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