#!/vol/perl/perl5.004_04/bin/perl -w -- # -*-Perl-*-
#
# murder
#
# murder just takes one arguement. It finds any processes with that
# string in them, and kills them.
#
use strict; # always
use Getopt::Long;
my($ps) = '/usr/bin/ps';
my($sleep) = 1;
#$sleep = 3 if ($username eq 'merlyn'); # ;-)
my($usage) = "Usage: murder [options] [command string]
-all Murder regardless of user id (need to be root)
-force Don't prompt me, just kill 'em
-nop Only show, don't actually kill
-help Display this help and exit
";
# parse options
use vars qw/$nop $all_users $force/;
GetOptions("nop" => \$nop,
"all" => \$all_users,
"force" => \$force,
"help" => sub { die "$usage\n"; },
) or die "$usage\n";
my($username) = getlogin || (getpwuid($<))[0] || die "Failed to obtain
+ username!";
# exit if no arguements
die "$usage\n" unless (@ARGV);
# Get list of processes
my(%pids);
foreach (`$ps -e -o "pid user args"`) {
my($searchstring) = join ' ',@ARGV;
if (/^(\d+)\s+(\S+)\s+(.*$searchstring.*)$/) {
next if ((! $all_users) && ($2 ne $username));
($pids{$1}{user},$pids{$1}{args}) = ($2,$3) unless /murder/;
}
}
die "No processes found\n" unless (%pids);
# We're killing stuff, so it's a good idea to have an "are you sure" p
+rompt
unless ($force) {
foreach (keys %pids) {
printf "%6u %-8s %s\n",$_,$pids{$_}{user},$pids{$_}{args};
}
print "Are you sure you want to kill these processes (y/n)? [n]:";
chomp(my $answer = <STDIN>);
die "Abort!\n" unless ($answer =~ /^y$/i);
}
# Kill 'em off
foreach my $pid (keys %pids) {
foreach my $signal (qw/2 1 15 9/) {
printf "Kill -$signal $pid ($pids{$pid}{user}:$pids{$pid}{args})\n
+";
unless ($nop) {
kill $signal => $pid;
sleep $sleep;
last unless (kill 0 => $pid);
}
}
}
|