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.
There's also the 'pidof' command, which can be used to kill with:
To get just one process when there are multiple with the same name, use -s for "single shot".
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
then run the following command to send the kill command
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.
with recent bash shell
How about using pgrep and pkill. They allow lot of options to select the processes.
Bash one-liner :-p
look at the -r option of the killall command!