views:

294

answers:

7

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.

+7  A: 

look at the -r option of the killall command!

lexu
Wouldn't that kill all the processes that match it?
Hintswen
@Hintswen: wasn't that the intended functionality, killing a process by name? In other words: I don't think I understand what you are asking!
lexu
I want to kill a single process with a certain name where there would be multiple processes with the same name. The one killed should be chosen at random. I don't want all of them killed.
Hintswen
Got it! Thanx! killall, as the name suggests, would be way too thorough!
lexu
+1  A: 

It sounded like you were already on the right track.

you can use the following perl script, save it as randomline.pl, which will return a random line from whats piped into it

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

then run the following command to send the kill command

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

You might also want to add in some checking, perhaps with an inverted grep for root to make sure you don't try to kill root level processes that match your process name.

whatsisname
Good idea with the checking.
Hintswen
+3  A: 

Bash one-liner :-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`
Steven Schlansker
Now thats cool. I love one liners! I can just save it in notepad and paste it in when I need it rather than saving it as a script.
Hintswen
you can make an alias rather than copypasting, don't you?
valya
-1 for using too many redundant tools + unnecessary piping.
I guess I could make an alias, but it's just something I wont use often enough and I would always be using it on a different server so not much point.
Hintswen
I don't see what's wrong with using "redundant" tools and "unnecessary" piping... it's not like this is a speed contest! I feel that having the pipeline readable and understandable at a quick glance is much more convenient than "golfing" my UNIX command lines... more assurance that I'll not accidentally issue a rm -r ~ or something by accident...
Steven Schlansker
This will find the "grep zsh" line, too, plus "man zshbuiltins" (for example). Some `sort` versions have a `-R` random sort option that would allow you to eliminate the `while` loop. You use `awk` to get a field in one place and `cut` to get one in another just for variety?
Dennis Williamson
@steven, you can program like that all your want man. I am just giving you an opinion based on experience, that that way of writing makes your script slow, especially if you are writing something more complex. When i was a newbie, i write like that too. After that i learn not to do the unnecessary stuffs.
+3  A: 

There's also the 'pidof' command, which can be used to kill with:

kill `pidof processname`

To get just one process when there are multiple with the same name, use -s for "single shot".

Martin B
wont work, multiple process with the same name... so they would all be killed at the same time.
Hintswen
Good point. I'll edit my comment to show how to pipe it to awk to single out one process.
Martin B
Actually there's a switch for it, -s. I edited my comment to add that option
Martin B
-s will not give a random one though.
Hintswen
A: 

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

+1  A: 

with recent bash shell

#!/bin/bash
declare -a pid
pid=( $(pidof myprocess) )
length=${#pid}
rnumber=$((RANDOM%length+1))
rand=$((rnumber-1))
kill ${pid[$rand]}
Why are you adding 1 then subtracting 1?
Dennis Williamson
add 1 so that rand number generated includes the upper bound. then its -1 because the pid array index starts from 0.
A: 

How about using pgrep and pkill. They allow lot of options to select the processes.

Saravanan