Kill a random process named

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

What would be the best way to do this?

I was thinking about using something like this:

ps aux | grep PROCESS-NAME

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

For my use, it’s not really necessary to be random if it kills one of the processes. Making it random just makes it better.

+3
source share
9 answers

Bash single line: -p

kill `ps auxww | grep zsh | awk '{print $2}' | while read line; do echo "$RANDOM $line"; done | sort | cut -d ' ' -f 2 | head -n 1`
+4
source

look at the -r option of killall !

+7
source

"pidof", :

kill `pidof processname`

, , -s " ".

+2

, .

perl script, randomline.pl, ,

#!/usr/bin/perl
srand (time ^ $$ ^ unpack "%L*", `ps axww | gzip`);
while (<>) { push(@_,$_); } print @_[rand()*@_];

, kill

kill `ps aux | grep PROCESS-NAME | perl randomline.pl | awk '{print $2}'`

, , grep root, , , .

0

awk.

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

for END pid,

0

bash

#!/bin/bash
declare -a pid
pid=( $(pidof myprocess) )
length=${#pid}
rnumber=$((RANDOM%length+1))
rand=$((rnumber-1))
kill ${pid[$rand]}
0

How about using pgrep and pkill. They allow you to use many options to select processes.

0
source

kill the process named "my_proc_name":

kill -9 `ps xf | grep my_proc_name | grep -v grep | cut -d " " -f 1`
0
source

It may be off topic, but I use it on Cygwin. Inspired by Lev Priymas in charge

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

or

ps -W | awk '$0~z,NF=1' z=calc.exe | xargs kill -f
0
source

Source: https://habr.com/ru/post/1725518/


All Articles