13

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?

20
pkill -f <application_na>

Will kill all the processes that contain the pattern <application_na> in their names.

man pkill

  • Works flawlessly each time every time – Raju Devidas Sep 25 '18 at 0:51
9

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
  • 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
0

killall can do that.

$ killall application_name
  • Is kill all allowing regular expression in an application name? – Łukasz D. Tulikowski 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

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.