How to kill all process with given name?

92,023

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
Share:
92,023

Related videos on Youtube

Łukasz D. Tulikowski
Author by

Łukasz D. Tulikowski

Updated on September 18, 2022

Comments

  • Łukasz D. Tulikowski
    Ł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
    Łukasz D. Tulikowski over 7 years
    Is kill all allowing regular expression in an application name?
  • drHogan
    drHogan over 7 years
    killall --regexp "appl.*me" Though there might be different killall implementations. See man killall.
  • Salem F
    Salem F almost 6 years
    killall 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
    Salem F almost 6 years
    this 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
    Daniel F over 5 years
    I have a problem with this where I get the output of kill -9 if no process matches
  • Toby Speight
    Toby Speight over 4 years
    Why the grep instead of using awk to do the test more correctly? grep will match names that include the target as substring, for example.
  • Toby Speight
    Toby Speight over 4 years
    Instead of the grep, you should be using awk to match on the name: ps -A | awk "\$4 == \"$1\" { print \$1; }"
  • Kamil Maciorowski
    Kamil Maciorowski over 3 years
    pkill --signal KILL …?
  • ibilgen
    ibilgen over 3 years
    I tried pkill -SIGKILL <pattern>, it didn't work. Can you give me a complete example of usage pkill with -9?
  • Kamil Maciorowski
    Kamil Maciorowski over 3 years
    In my Ubuntu pkill -9 sleep or pkill --signal KILL sleep forcefully kills all my sleep processes.