3

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.

Zombo
  • 1
  • 62
  • 391
  • 407
Hintswen
  • 3,987
  • 12
  • 40
  • 45
  • 1
    You can get better answers for this question on ServerFault. There's a near-duplicate of this question at http://serverfault.com/questions/71360/find-and-kill-old-processes – Chip Uni Dec 13 '09 at 07: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 07:39

9 Answers9

7

look at the -r option of the killall command!

lexu
  • 8,766
  • 5
  • 45
  • 63
  • Wouldn't that kill all the processes that match it? – Hintswen Dec 13 '09 at 08: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
4

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
  • 37,580
  • 14
  • 81
  • 100
  • 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 07:36
  • you can make an alias rather than copypasting, don't you? – Valentin Golev Dec 13 '09 at 07:55
  • 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 08: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 09:06
  • 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 Dec 13 '09 at 12:25
  • @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. – ghostdog74 Dec 13 '09 at 13:09
2

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
  • 29
  • 2
0

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
Community
  • 1
  • 1
Zombo
  • 1
  • 62
  • 391
  • 407
0

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
  • 5,872
  • 2
  • 20
  • 27
0

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

ghostdog74
  • 327,991
  • 56
  • 259
  • 343
0

with recent bash shell

#!/bin/bash
declare -a pid
pid=( $(pidof myprocess) )
length=${#pid}
rnumber=$((RANDOM%length+1))
rand=$((rnumber-1))
kill ${pid[$rand]}
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
0

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

0

kill process with name "my_proc_name" :

kill -9 `ps xf | grep my_proc_name | grep -v grep | cut -d " " -f 1`
Lev
  • 29
  • 2