How to kill all process with given name?
Solution 1
pkill -f 'PATTERN'
Will kill all the processes that the pattern PATTERN
matches. With the -f
option, the whole command line (i.e. including arguments) will be taken into account. Without the -f
option, only the command name will be taken into account.
See also man pkill
on your system.
Solution 2
The problem is that ps -A | grep <application_name> | xargs -n1
returns output like this
19440
?
00:00:11
<application_name>
21630
?
00:00:00
<application_name>
22694
?
00:00:00
<application_name>
You can use awk
to a get first a column of ps
output.
ps -A | grep <application_name> | awk '{print $1}' | xargs -n1
Will return list of PIDs
19440
21630
22694
And adding kill -9 $1
you have a command which kills all PIDs
ps -A | grep <application_name> | awk '{print $1}' | xargs kill -9 $1
Solution 3
killall
can do that.
$ killall application_name
Related videos on Youtube
Łukasz D. Tulikowski
Updated on September 18, 2022Comments
-
Łukasz D. Tulikowski over 1 year
I run command
ps -A | grep <application_name>
and getting list of process like this:19440 ? 00:00:11 <application_name> 21630 ? 00:00:00 <application_name> 22694 ? 00:00:00 <application_name>
I want to kill all process from the list:
19440
,21630
,22694
.I have tried
ps -A | grep <application_name> | xargs kill -9 $1
but it works with errors.kill: illegal pid ? kill: illegal pid 00:00:00 kill: illegal pid <application_name>
How can I do this gracefully?
-
Łukasz D. Tulikowski over 7 yearsIs kill all allowing regular expression in an application name?
-
drHogan over 7 years
killall --regexp "appl.*me"
Though there might be different killall implementations. Seeman killall
. -
Salem F almost 6 yearskillall not enough sometimes I need to send it three time to kill the process , and even fail to kill it , the only fast working solution fo me is
kill -9 pid
I think @ŁukaszD.Tulikowski is the best working solution specially for bash scripts . -
Salem F almost 6 yearsthis is perfect I test it on bash script it's kills the processer immediatly with no errors + even if the process is'nt started it shows no errors which is what I want , here example of ffmpeg processer killer ,
nano /usr/bin/ffmpegk
. . . .ps -A | grep ffmpeg | awk '{print $1}' | xargs kill -9 $1
. . . .chmod a+rx /usr/bin/ffmpegk
-
Daniel F over 5 yearsI have a problem with this where I get the output of
kill -9
if no process matches -
Toby Speight over 4 yearsWhy the
grep
instead of usingawk
to do the test more correctly?grep
will match names that include the target as substring, for example. -
Toby Speight over 4 yearsInstead of the
grep
, you should be usingawk
to match on the name:ps -A | awk "\$4 == \"$1\" { print \$1; }"
-
Kamil Maciorowski over 3 years
pkill --signal KILL …
? -
ibilgen over 3 yearsI tried pkill -SIGKILL <pattern>, it didn't work. Can you give me a complete example of usage pkill with -9?
-
Kamil Maciorowski over 3 yearsIn my Ubuntu
pkill -9 sleep
orpkill --signal KILL sleep
forcefully kills all mysleep
processes.