97

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,374
  • 1
  • 25
  • 48
user567879
  • 4,809
  • 19
  • 68
  • 102

9 Answers9

186
kill $(ps -e | grep dmn | awk '{print $1}')
Daniel Persson
  • 2,136
  • 1
  • 12
  • 11
  • 9
    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
55

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
  • 37,681
  • 8
  • 99
  • 131
20

You could use

pkill dmn 

if your system has the pkill command.

unutbu
  • 777,569
  • 165
  • 1,697
  • 1,613
  • 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
4

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

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

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.
  • 2,914
  • 2
  • 28
  • 63
4

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
  • 15,944
  • 193
  • 67
  • 96
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
  • 7,568
  • 1
  • 25
  • 53