37

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?

0
57
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.

0
20

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
3
  • 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
    – Salem F
    May 21 '18 at 12:26
  • I have a problem with this where I get the output of kill -9 if no process matches
    – Daniel F
    Aug 19 '18 at 12:46
  • 1
    Instead of the grep, you should be using awk to match on the name: ps -A | awk "\$4 == \"$1\" { print \$1; }" Nov 14 '19 at 18:13
1

killall can do that.

$ killall application_name
3
  • Is kill all allowing regular expression in an application name? Oct 13 '16 at 0:59
  • killall --regexp "appl.*me" Though there might be different killall implementations. See man killall.
    – rudimeier
    Oct 13 '16 at 1:02
  • 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
    May 21 '18 at 12:16
0

pkill sends SIGTERM in default, and in my case pkill -f <some_pattern> did'nt kill my processes. I recommend below command for such cases, which worked perfectly for me!

kill -9 $(pgrep -f somepattern)

I also recommend to see which processes match before running kill command

pgrep -af somepattern
3
  • pkill --signal KILL …? Sep 24 '20 at 21:24
  • I tried pkill -SIGKILL <pattern>, it didn't work. Can you give me a complete example of usage pkill with -9?
    – ibilgen
    Sep 25 '20 at 0:08
  • In my Ubuntu pkill -9 sleep or pkill --signal KILL sleep forcefully kills all my sleep processes. Sep 25 '20 at 4:24
-2

My approach is similiar to @Łukasz D. Tulikowski's. Instead of using grep <application_name>; I have used grep "[a]pplication_name", which does not match with its own process command line.

The square bracket expression is part of the grep's character class pattern matching. (Reference).


You can use awk to a get first a column of ps output.

ps -A | grep "[a]pplication_na" | awk '{print $2}' | xargs -n1

Will return list of PIDs

7644
407
406

Later adding kill -9 $1 you have a command, which kills all PIDs

kill -9 $(ps aux | grep "[a]pplication_na" | awk '{print $2}')
1
  • 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. Nov 14 '19 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.