How to give arguments to kill via pipe

2019-01-30 08:15发布

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.

9条回答
成全新的幸福
2楼-- · 2019-01-30 08:21
kill $(ps -e | grep dmn | awk '{print $1}')
查看更多
时光不老,我们不散
3楼-- · 2019-01-30 08:21

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.

查看更多
We Are One
4楼-- · 2019-01-30 08:22

You can also use killall:

killall dmn
查看更多
We Are One
5楼-- · 2019-01-30 08:27

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

查看更多
爷、活的狠高调
6楼-- · 2019-01-30 08:28

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.

查看更多
Root(大扎)
7楼-- · 2019-01-30 08:29

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)
查看更多
登录 后发表回答