How to give arguments to kill via pipe

2019-01-30 08:38发布

问题:

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.

回答1:

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


回答2:

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.



回答3:

You could use

pkill dmn 

if your system has the pkill command.



回答4:

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

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


回答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)


回答6:

You can also use killall:

killall dmn


回答7:

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.



回答8:

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.



回答9:

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