如何论证通过管道杀人

我需要搜索某个进程并终止该进程:

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

其中进程名称为 dmn。但是没有用。如何通过名称和 kill查找进程。

79131 次浏览

You could use

pkill dmn

if your system has the pkill command.

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

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.

You can also use killall:

killall dmn
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.

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

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

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

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)

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.