#!/usr/local/bin/perl # pryan@stx.com (patrick m. ryan) # syntax: # kp [-n] [-] pattern1 [pattern2...] #eval 'exec perl $0 -S ${1+"$@"}' # if $running_under_some_shell; $sig = 1; # SIGHUP by default # parse command line while ($ARGV[0] =~ /^-(.+)/) { $arg = $1; if ($arg eq "n") { # not really $not_really=1; } elsif ($arg =~ /^\d+$/) { # signal argument $sig=$arg; } else { print STDERR "$ARGV[0]: unrecognized option\n"; exit 1; } shift @ARGV; } # copy arguments @patt = @ARGV; unless (@patt) { print STDERR "no search pattern specified\n"; exit 1; } open(PS, "ps -acugx |"); # open(PS, "ps -aef |"); # ps -aef for SYSV $_ = ; # we hope this is the header if ((/UID/) || (/USER/)) # divine some info from header { chop; @head = split; $nf = $#head; # foreach (@head) { printf "%2d %s\n",$k,$_; ++$k; } ## for ($i=0; $i <= $nf; $i++) $i=0; foreach (@head) { if (/UID/o || /USER/o) { $i_user = $i; } elsif (/\bPID/o) { $i_pid = $i; } elsif (/COM/o) { $i_cmd = $i; } $i++; } } else { print "could not decipher header line\n"; exit; } #print "i_user = $i_user\n"; #print "i_cmd = $i_cmd\n"; # assume real processes start here if ($i_cmd <= 0) { $i_cmd = $nf; } #print "i_pid = $i_pid, i_user = $i_user, i_cmd = $i_cmd\n"; @victims = (); while () # loop over the processes { chop; s/^\s+//; s/\s+$//; @t = split(/\s+/,$_,$nf+1); # split up the line into fields if ($t[$i_pid] =~ /$$/) { next; } # don't kill myself ' #print "user = ",$t[$i_user],"\n"; #print "cmd = ",$t[$i_cmd],"\n"; #print "pid = ",$t[$i_pid],"\n"; foreach (@patt) { if ($t[$i_user] =~ /$_/ ) { push(@victims,$t[$i_pid]); } if ($t[$i_cmd] =~ /$_/ ) { push(@victims,$t[$i_pid]); } } } # helpful output statement unless (@victims) { print STDERR "Not have way.\n"; exit 0; } #print "i'm crushing your head!!!: ",@victims,"\n"; print "killing processes "; foreach (@victims) { print "$_ "; } print " ...not" if $not_really; print "\n"; # send the signal unless ($not_really) { kill $sig, @victims; if ($!) { print "error: $!\n"; } } exit 0; # Local Variables: # mode: perl # End: