Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to kill all processes matching a name?

Say I want to kill every process containing the word amarok. I can print out the commands I want to execute. But how do I actually make the shell execute them. ie.

ps aux | grep -ie amarok | awk '{print "kill -9 " $2}' Output: kill -9 3052 kill -9 3071 kill -9 3076 kill -9 3077 kill -9 3079 kill -9 3080 kill -9 3082 kill -9 3083 kill -9 3084 kill -9 3085 kill -9 3086 kill -9 3087 kill -9 3088 kill -9 3089 kill -9 4031 
like image 429
PuercoPop Avatar asked Jun 17 '11 04:06

PuercoPop


People also ask

How do I kill all processes with the same name?

You can use the pkill command. Note that myapp2 won't be killed as it has a different name. Show activity on this post. it will kill all processes starting with myap.

How do I kill all processes belonging to a user?

Killall command allows you to terminate all the processes owned by a specific user. To do this, use the -u flag.

How do I kill a group of processes?

If it is a process group you want to kill, just use the kill(1) command but instead of giving it a process number, give it the negation of the group number. For example to kill every process in group 5112, use kill -TERM -- -5112 .


2 Answers

From man 1 pkill

-f     The pattern is normally only matched against the process name.        When -f is set, the full command line is used. 

Which means, for example, if we see these lines in ps aux:

apache   24268  0.0  2.6 388152 27116 ?        S    Jun13   0:10 /usr/sbin/httpd apache   24272  0.0  2.6 387944 27104 ?        S    Jun13   0:09 /usr/sbin/httpd apache   24319  0.0  2.6 387884 27316 ?        S    Jun15   0:04 /usr/sbin/httpd 

We can kill them all using the pkill -f option:

pkill -f httpd 
like image 135
Tim Bielawa Avatar answered Oct 03 '22 07:10

Tim Bielawa


ps aux | grep -ie amarok | awk '{print $2}' | xargs kill -9  

xargs(1): xargs -- construct argument list(s) and execute utility. Helpful when you want to pipe in arguments to something like kill or ls or so on.

like image 21
Nektarios Avatar answered Oct 03 '22 09:10

Nektarios