338

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
codeforester
  • 39,467
  • 16
  • 112
  • 140
PuercoPop
  • 6,707
  • 4
  • 30
  • 40
  • 2
    Add `| sh -x` after the remainder of your command line? – Jonathan Leffler Jun 17 '11 at 05:00
  • 8
    never ever use `kill -9` on a process see [process management][1] [1]: http://mywiki.wooledge.org/ProcessManagement?highlight=%28%5CbCategoryUnix%5Cb%29#I.27m_trying_to_kill_-9_my_job_but_blah_blah_blah... – Fredrik Pihl Jun 17 '11 at 08:14
  • Adding to the above comment that not only is the kill -9 a bad idea but also the fact that you are trying to kill multiple processes via their name. There is nothing unique about the name of a process and hence is a bad candidate for identification. – Raul- Dec 02 '14 at 21:47
  • Will it kill `amarokx` process too? I pkill vi, it kills `supervisorctl` which contains `vi' in the word. – Gank Dec 12 '15 at 02:45
  • 6
    That article on not using `-9` is pretty unhelpful - is there any better information on why people shouldn't use it? It just says "dont, and fire people who use it"; but all I've ever seen everywhere else is that `kill -9 {pid}` is how to kill something... – dwanderson Sep 04 '18 at 18:10
  • 4
    from `man kill`: kill -9 sends the SIGKILL signal. This is a `non-catchable, non-ignorable kill`. Meaning that the application will not get to clean up and properly save progress. – AlexeyGy Dec 12 '19 at 13:22
  • 2
    Usually when I want to kill a process, its because its stuck, and its not responding to any other kill commands. The only time I actually use the kill command is if something is horribly frozen and then I force it with -9. – Rob Apr 16 '20 at 09:55
  • `kill -9` may result in large amounts of garbage in the filesystem and in memory. Do it enough, and your system will choke. – Lee Goddard Aug 11 '22 at 07:35

11 Answers11

551

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
Tim Bielawa
  • 6,935
  • 2
  • 17
  • 11
265
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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Nektarios
  • 10,173
  • 8
  • 63
  • 93
  • 4
    Points for mentioning xargs - all this pgrep / pkill stuff is all very well, but xargs is a general purpose tool, and standard on every linux/unix. – chrisdowney Jun 18 '11 at 12:44
  • 4
    "kill: No such process" – Cerin Jan 13 '14 at 21:58
  • 8
    Cerin that's because it's trying to kill the grep process as well, which already finished. It should be fine. – Costi Muraru Jun 18 '14 at 23:47
  • 9
    There's a little trick to avoiding capturing the grep process: ps aux | grep -ie [a]marok | awk '{print $2}' | xargs kill -9 – RCross Sep 25 '14 at 12:09
  • 12
    you can also add another grep call to filter out grep (I find this easier to read/remember) `ps aux | grep -ie amarok | grep -v grep | awk '{print $2}' | xargs kill -9` (`grep -v` stands for `grep --invert`) – floer32 Oct 03 '14 at 03:01
  • 1
    No no no, all of that is [useless use of `grep`](http://www.iki.fi/era/unix/award.html#grep). You want `ps aux | awk '/amarok/ { print $2 }' | xargs kill` (mostly without the `-9` as suggested elsewhere, and also on the linked page). – tripleee Sep 15 '16 at 14:57
  • Take care, the process ids (pids) are not always located in the second column (which is used in the awk code by using `$2`), but in the first column. Hence, use `$1`. This holds for git-bash on Windows for example. – MichaelHuelsen Aug 16 '17 at 05:21
  • won't kill certain orphaned processes – mjs Jan 20 '20 at 22:15
61

use pgrep

kill -9 $(pgrep amarok)
Eric Fortis
  • 16,372
  • 6
  • 41
  • 62
39

The safe way to do this is:

pkill -f amarok
John Alexander Betts
  • 4,718
  • 8
  • 47
  • 72
  • 1
    To add to this, I added the `-l` flag. I actually just aliased `pkill` to `pkill -fl` so I can run something like `pkill jboss` and it will spit out the process that it just killed. – nland Dec 10 '15 at 16:45
  • Will it kill `amarokx` process too? I pkill vi, it kills `supervisorctl` which contains `vi' in the word. – Gank Dec 12 '15 at 02:44
  • How is this different from the top (Tim's) answer? Just matches the example name, otherwise looks identical? @ndland `pkill: invalid option -- 'l'` - pkill from procps-ng 3.3.12 – Xen2050 Oct 25 '18 at 06:31
21

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.

androidyue
  • 952
  • 10
  • 11
  • This only matches by name, not full command line. Definitely not the same as searching `ps aux`'s output – Xen2050 Oct 25 '18 at 07:06
16

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!

user79878
  • 775
  • 9
  • 13
5

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

anneb
  • 5,510
  • 2
  • 26
  • 26
Victor Marrerp
  • 157
  • 1
  • 4
4

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.

Chen Levy
  • 15,438
  • 17
  • 74
  • 92
3

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}')     
dandrews
  • 2,995
  • 1
  • 20
  • 22
0

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

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>
Tony O'Hagan
  • 21,638
  • 3
  • 67
  • 78