Kill random process with name

2019-05-13 22:00发布

I want a way to kill a random process with a name (eg a random perl process).

What would be the best way of doing this?

I was thinkign of using something like this:

ps aux | grep PROCESS-NAME

to a file, then find a random line number, get the second column (process ID?) and kill that.

For my use it doesn't actually need to be a random one, as long as it kills one of the processes. Making it random just makes it better.

9条回答
做个烂人
2楼-- · 2019-05-13 22:30

kill process with name "my_proc_name" :

kill -9 `ps xf | grep my_proc_name | grep -v grep | cut -d " " -f 1`
查看更多
你好瞎i
3楼-- · 2019-05-13 22:31

Maybe off topic, but I use this on Cygwin. Inspired by Lev Victorovich Priyma’s answer

ps -W | awk '/calc.exe/,NF=1' | xargs kill -f

or

ps -W | awk '$0~z,NF=1' z=calc.exe | xargs kill -f
查看更多
三岁会撩人
4楼-- · 2019-05-13 22:39

just kill and awk.

kill $(ps -eo cmd,pid|awk '/zsh/&&!/awk/{pid[$NF]}END{for(i in pid){print i;exit}}')

the for loop in the END block will give you you a random pid to kill

查看更多
登录 后发表回答