Take the 2-minute tour ×

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.

share|improve this question
You can get better answers for this question on ServerFault. There's a near-duplicate of this question at serverfault.com/questions/71360/find-and-kill-old-processes –  Chip Uni Dec 13 '09 at 7:35
Aah ok, I never thought to look on there. Only just started using ServerFault last week. I tried checking Google and couldn't find it. –  Hintswen Dec 13 '09 at 7:39
add comment

9 Answers

up vote 3 down vote accepted

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`
share|improve this answer
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 Dec 13 '09 at 7:36
you can make an alias rather than copypasting, don't you? –  Valentin Golev Dec 13 '09 at 7:55
-1 for using too many redundant tools + unnecessary piping. –  ghostdog74 Dec 13 '09 at 8:00
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 Dec 13 '09 at 8:23
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 Dec 13 '09 at 9:06
show 2 more comments

look at the -r option of the killall command!

share|improve this answer
Wouldn't that kill all the processes that match it? –  Hintswen Dec 13 '09 at 8:20
@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 Dec 14 '09 at 19:51
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 Dec 14 '09 at 21:16
Got it! Thanx! killall, as the name suggests, would be way too thorough! –  lexu Dec 14 '09 at 21:39
add comment

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".

share|improve this answer
wont work, multiple process with the same name... so they would all be killed at the same time. –  Hintswen Dec 13 '09 at 8:19
Good point. I'll edit my comment to show how to pipe it to awk to single out one process. –  Martin B Dec 13 '09 at 18:24
Actually there's a switch for it, -s. I edited my comment to add that option –  Martin B Dec 13 '09 at 18:28
-s will not give a random one though. –  Hintswen Dec 14 '09 at 5:59
add comment

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

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.

share|improve this answer
Good idea with the checking. –  Hintswen Dec 13 '09 at 7:37
add comment

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

share|improve this answer
add comment

with recent bash shell

declare -a pid
pid=( $(pidof myprocess) )
kill ${pid[$rand]}
share|improve this answer
Why are you adding 1 then subtracting 1? –  Dennis Williamson Dec 13 '09 at 12:09
add 1 so that rand number generated includes the upper bound. then its -1 because the pid array index starts from 0. –  ghostdog74 Dec 13 '09 at 13:01
add comment

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

share|improve this answer
add comment

kill process with name "my_proc_name" :

kill -9 `ps xf | grep my_proc_name | grep -v grep | cut -d " " -f 1`
share|improve this answer
add comment

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


ps -W | awk '$0~a, NF=1' a=calc.exe | xargs kill -f
share|improve this answer
add comment

Your Answer


