How to give arguments to kill via pipe

LinuxBashShellCommand LineCommand Line-Arguments

Linux Problem Overview


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.

Linux Solutions


Solution 1 - Linux

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

Solution 2 - Linux

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.

Solution 3 - Linux

You could use

pkill dmn 

if your system has the pkill command.

Solution 4 - Linux

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

kill $(ps | awk '/dmn/{print $1}')

Solution 5 - Linux

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)

Solution 6 - Linux

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.

Solution 7 - Linux

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.

Solution 8 - Linux

You can also use killall:

killall dmn

Solution 9 - Linux

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser567879View Question on Stackoverflow
Solution 1 - LinuxDaniel PerssonView Answer on Stackoverflow
Solution 2 - LinuxjcolladoView Answer on Stackoverflow
Solution 3 - LinuxunutbuView Answer on Stackoverflow
Solution 4 - LinuxpedmistonView Answer on Stackoverflow
Solution 5 - LinuxNathan F.View Answer on Stackoverflow
Solution 6 - LinuxNotVeryPythonicView Answer on Stackoverflow
Solution 7 - LinuxNishantView Answer on Stackoverflow
Solution 8 - LinuxSKiView Answer on Stackoverflow
Solution 9 - LinuxDeepak SharmaView Answer on Stackoverflow