File: //scripts.20110531.215904.25158/ckillall2
#!/usr/bin/perl
if (! -e "/proc/1") {
print "Critical Error: /proc is not mounted)!\n";
print "Attempting to mount /proc...";
system("mount","-t","procfs","/proc","/proc");
print "Done\n";
}
if (! -e "/proc/1") {
print "Critical Error: /proc is still not mounted!\n";
exit;
}
my $pid = $$;
my $signal = $ARGV[0];
my $deadcmd = $ARGV[1];
$signal =~ s/-//g;
if ($signal eq "HUP") { $signal = '1'; }
if ($signal eq "KILL") { $signal = '9'; }
if ($signal eq "TERM") { $signal = '15'; }
if ($signal eq "USR1") { $signal = '10'; }
if ($signal !~ /^\d+/ || $deadcmd eq "") {
print "Usage: ckillall -signal command\n";
exit;
}
if ($deadcmd eq "init") {
print "PANIC! Attempted to kill init!\n";
exit;
}
opendir(PROC,"/proc");
@PROCS=readdir(PROC);
closedir(PROC);
@PROCS=grep(/^\d+$/, @PROCS);
@PROCS=grep(!/^${pid}$/, @PROCS);
foreach my $proc (@PROCS) {
open(CMDLINE,"/proc/$proc/cmdline");
$cmd = <CMDLINE>;
@CMDS = split(/\0/, $cmd);
$processname = getbasename($CMDS[0]);
$processename = getbasename($CMDS[1]);
print "pid $proc is $processname ($processename)\n";
if ($processname eq $deadcmd ||
($processname eq "sh" && $processename eq $deadcmd) ||
($processname eq "perl" && $processename eq $deadcmd)) {
kill($signal,$proc);
# foreach my $cmd (@CMDS) {
# print "[$cmd]";
# }
# print "\n";
}
close(CMDLINE);
}
sub getbasename {
my($name) = @_;
$name =~ s/^-//g;
if ($name =~ /\//) {
my @BASE = split(/\//, $name);
return($BASE[$#BASE]);
} elsif ($name =~ /\s/) {
$name =~ /^(\S+)/;
if ($1 eq "") {
return(-1);
}
return($1);
} else {
return($name);
}
}