#!/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);
}
}
}
In reply to Murder
by Shendal
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.