53

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

5 Answers 5

86
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.

2
  • Assuming pkill is installed, of course - on Debian it's in the procps package. Oct 2, 2022 at 9:40
  • 1
    I need to get this tattooed on the back of my hand.
    – rinogo
    May 19, 2023 at 22:40
28

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
4
  • 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, 2018 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, 2018 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, 2019 at 18:13
  • could we use ps auxww instead of ps -A?
    – alper
    Oct 1, 2022 at 23:25
2

killall can do that.

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

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, 2020 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, 2020 at 0:08
  • In my Ubuntu pkill -9 sleep or pkill --signal KILL sleep forcefully kills all my sleep processes. Sep 25, 2020 at 4:24
-2

And I have used -w flag for ps to capture wide output in case not to miss any process name.

ps auxww | grep -E "[a]pplication_name" | awk '{print $2}'

Will return list of PIDs

7644
407
406

Next, if there is valid output the results piped into xargs kill -9 command, to kill all correspondings PIDs.

ps auxww | grep -E "[a]pplication_name" | awk '{print $2}' | ifne xargs kill -9
9
  • 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, 2019 at 18:09
  • @TobySpeight sorry I did not understand what you mean by Why the grep instead of using awk?
    – alper
    Oct 1, 2022 at 23:28
  • 1
    Two reasons: no need for an extra process, since we're already running awk, and the grep will match more than we want - e.g. if I search for emacs, I also get emacsclient processes. And with a search pattern that matches my username, I get many other processes. Consider something like ps auxww | awk 'gensub(".*/","",1,$11) == "'"$prog_name"'" {print $2}' for exact matching. The whole approach is flawed on platforms (such as Linux) that let applications write to the process name area, anyway. You're better off installing the procps package, which provides pgrep and pkill. Oct 2, 2022 at 9:38
  • @TobySpeight Thanks for the detailed explaination. But it only finds exact matching process names like you mention. Instead can we apply for sub-matching words like prog_n*?
    – alper
    Oct 2, 2022 at 11:17
  • 1
    If that's what you want, just use a regular-expression match (~) instead of equality match (==). Oct 2, 2022 at 11:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .