104

I need to search for a certain process and kill that process. I wrote a command like this:

ps -e | grep dmn | awk '{print $1}' | kill

Where the process name is dmn. But it is not working. How can I find processes by name and kill them.

Will
  • 4,585
  • 1
  • 26
  • 48
user567879
  • 5,139
  • 20
  • 71
  • 105

9 Answers9

199
kill $(ps -e | grep dmn | awk '{print $1}')
Daniel Persson
  • 2,265
  • 1
  • 13
  • 12
  • 10
    Use the newer `$()` syntax: `kill $(ps -e | grep dmn | awk '{print $1}')`. – Stratus3D Feb 18 '15 at 16:39
  • 11
    fwiw for others, i had to modify this answer to get it to work: `kill $(ps -efw | grep dmn | grep -v grep | awk '{print $2}')` not sure why and dont care enough too look further into it. – joshweir Jan 22 '16 at 06:20
  • 2
    @joshweir It is because otherwise `kill` tried to kill even the 'grep' process which was trying to search for the pattern – meain Jun 18 '16 at 06:19
  • Wouldn't it be required `\n` after each awk, like in `awk '{print $1"\n"}'` ? – Sopalajo de Arrierez Jun 18 '16 at 18:42
  • 2
    As always, [avoid the useless `grep`](http://www.iki.fi/era/unix/award.html#grep); `kill $(ps -e | awk '/dmn/ { print $1 }'` (though of course, also, don't reinvent the wheel: `pkill dmn`). – tripleee May 17 '21 at 14:57
58

In case there are multiple processes that you want to remove you can use this:

ps -efw | grep dmn | grep -v grep | awk '{print $2}' | xargs kill

Note: You need to remove grep process itself from the output, that's why grep -v grep is used.

jcollado
  • 39,419
  • 8
  • 102
  • 133
22

You could use

pkill dmn 

if your system has the pkill command.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • 2
    +1 for pkill instead of killall. It's available on multiple platforms without different "meaning" ([killall](http://bama.ua.edu/cgi-bin/man-cgi?killall) on Solaris is equivalent to [killall5](http://linux.die.net/man/8/killall5) on Linux for example - That is, kill *ALL* processes) – plundra Dec 28 '11 at 09:24
5

Just adding on others, but I like using awk's regex features capacity:

kill $(ps | awk '/dmn/{print $1}')
pedmiston
  • 309
  • 4
  • 11
5

If you have the pidof command on your system ( I know shells such as ZSH come with this by default, unless I'm mistaken), you could do something like.

kill -9 $(pidof dmn)
Nathan F.
  • 3,250
  • 3
  • 35
  • 69
5

You might not need pipe for this, if you have pidof command and know the image name, I did it like this:

kill $(pidof synergyc)

$() I understand this as it converts that output to a variable that kill can use, essentially like pipe would do. Shorter and easier to understand than some other options but also maybe less flexible and more direct.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
3
for procid in $(ps -aux | grep "some search" | awk '{print $2}'); do kill -9 $procid; done

hello friends .. we can do it using for loop .

"Some search" is here any process name you want to search, for example "java" so let say count of java process is 200+ so killing one by one will be too typical .

so you can use above command.

Thanks.

Nishant
  • 41
  • 1
2

Use pgrep with -f option. kill $(pgrep -f dmn)

Deepak Sharma
  • 156
  • 1
  • 9
2

You can also use killall:

killall dmn
SKi
  • 8,007
  • 2
  • 26
  • 57