How to kill all processes matching a name?

ShellCommand LineKill

Shell Problem Overview


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

Shell Solutions


Solution 1 - Shell

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

Solution 2 - Shell

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.

Solution 3 - Shell

use pgrep

kill -9 $(pgrep amarok)

Solution 4 - Shell

The safe way to do this is:

pkill -f amarok

Solution 5 - Shell

I think this command killall is exactly what you need. The command is described as "kill processes by name".It's easy to use.For example

killall chrome

This command will kill all process of Chrome.Here is a link about killall command

http://linux.about.com/library/cmd/blcmdl1_killall.htm

Hope this command could help you.

Solution 6 - Shell

pkill -x matches the process name exactly.

pkill -x amarok

pkill -f is similar but allows a regular expression pattern.

Note that pkill with no other parameters (e.g. -x, -f) will allow partial matches on process names. So "pkill amarok" would kill amarok, amarokBanana, bananaamarok, etc.

I wish -x was the default behavior!

Solution 7 - Shell

If you want to execute the output of a command, you can put it inside $(...), however for your specific task take a look at the killall and pkill commands.

Solution 8 - Shell

try kill -s 9 `ps -ef |grep "Nov 11" |grep -v grep | awk '{print $2}'` To kill processes of November 11 or kill -s 9 `ps -ef |grep amarok|grep -v grep | awk '{print $2}'` To kill processes that contain the word amarok

Solution 9 - Shell

You can also evaluate your output as a sub-process, by surrounding everything with back ticks or with putting it inside $():

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

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

Solution 10 - Shell

Maybe adding the commands to executable file, setting +x permission and then executing?

ps aux | grep -ie amarok | awk '{print "kill -9 " $2}' > pk;chmod +x pk;./pk;rm pk

Solution 11 - Shell

If you're using cygwin or some minimal shell that lacks killall you can just use this script:

killall.sh - Kill by process name.

#/bin/bash
ps -W | grep "$1" | awk '{print $1}' | xargs kill --

Usage:

$ killall <process name>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPuercoPopView Question on Stackoverflow
Solution 1 - ShellTim BielawaView Answer on Stackoverflow
Solution 2 - ShellNektariosView Answer on Stackoverflow
Solution 3 - ShellEric FortisView Answer on Stackoverflow
Solution 4 - ShellJohn Alexander BettsView Answer on Stackoverflow
Solution 5 - ShellandroidyueView Answer on Stackoverflow
Solution 6 - Shelluser79878View Answer on Stackoverflow
Solution 7 - ShellChen LevyView Answer on Stackoverflow
Solution 8 - ShellVictor MarrerpView Answer on Stackoverflow
Solution 9 - ShelldandrewsView Answer on Stackoverflow
Solution 10 - Shellvova7865View Answer on Stackoverflow
Solution 11 - ShellTony O'HaganView Answer on Stackoverflow