8

How to stop a bunch of processes on unix fast.

I can use ps/top, kill.

If the number of processes is very large, I use shell script. Are there other ways, only one or two unix commands can do it well ?

3
  • Do you have common name prefix for your processes? – Prince John Wesley Oct 27 '11 at 5:08
  • 1
    The danger here is that when you start killing processes fast, you might kill something you didn't intend to kill. – ObscureRobot Oct 27 '11 at 5:11
  • telinit 0, or kill -9 1 works well for killing ALL the processes... – lornix Oct 27 '11 at 9:37
10

My choice is pkill/pgrep

1
  • Great! Just to give an example: If you are looking to kill all curl processes, first run pgrep -a curl to look at all the processes that will be killed by pkill. If everything looks good, use pkill curl. – arun Feb 24 '20 at 14:16
3

Here's a little on-line bash script I use:

ps auxwww | grep -v grep | grep -i $1 | awk '{print $2}' | xargs kill -9

I kills all processes whose name contains the first parameter (case insensitive). The breakdown of each part is:

  • ps auxwww prints all processes with very wide "command" field
  • grep -v grep is there to exclude itself from the killing
  • grep -i $1 matches all lines that contain the first parameter
  • awk '{print $2}' grabs all the process ids
  • xargs kill -9 kills all the selected processes
3
  • +1 Love a good pipeline, especially one using xargs. – Ray Toal Oct 27 '11 at 5:12
  • i prefer to use fgrep(no regex) – Prince John Wesley Oct 27 '11 at 5:14
  • 8
    This script is overly complex compared to pkill -9 -f $1. Also, don't advise using signal 9 (KILL). It is a last resort way of stopping a process, not one that should be used routinely. – jlliagre Oct 27 '11 at 5:34
1

Psdoom is really the only way to kill a process. http://psdoom.sourceforge.net/

0

Very Fast Way to Kill Multiple Processes (without having to write a script)

USAGE: ./autokill.kl.sh <proc-name> => This will give you 5 seconds to change your mind and control-C out of the program.

USAGE: ./autokill.kl.sh <proc-name> now => This will immediately kill all processes matching the string/pattern you specified!!!

MODEL: ./autokill.kl.sh httpd

MODEL: ./autokill.kl.sh httpd now

MODEL: ./autokill.kl.sh httpd view => This will only show you the list of processes matching the pattern you specified.

Script can be directly downloaded here.

0

I really prefer using the pgrep/pkill command ad here is an example:

[root@testrhel7 ~]# pgrep -l http
2967 httpd
2968 httpd
2969 httpd
2970 httpd
2971 httpd
2972 httpd
[root@testrhel7 ~]#
[root@testrhel7 ~]# pkill -e http
httpd killed (pid 2967)
httpd killed (pid 2968)
httpd killed (pid 2969)
httpd killed (pid 2970)
httpd killed (pid 2971)
httpd killed (pid 2972)
[root@testrhel7 ~]#

Of course You can use a for cycle:

[root@testrhel7 ~]# ps -ef  |grep http| grep -v grep
root       2989      1  0 16:06 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     2990   2989  0 16:06 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     2991   2989  0 16:06 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     2992   2989  0 16:06 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     2993   2989  0 16:06 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     2994   2989  0 16:06 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
[root@testrhel7 ~]#

[root@testrhel7 ~]# for i in $(ps -ef | awk '/http/ {print $2}'); do kill -9 $i; done 

I hope that was helpful, happy killing.

Your Answer

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