# Copyright (c) 2004 Shirshanka Das # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, write to # the Free Software Foundation, 59 Temple Place - Suite 330, # Boston, MA 02111-1307, USA. # # Shirshanka Das # Computer Science Department # University of California, Los Angeles # 3803A Boelter Hall # Los Angeles, CA 90095-1596 # U.S.A. # mailto:shanky@cs.ucla.edu # http://www.cs.ucla.edu/~shanky #!/usr/bin/perl #Takes in a pattern to be grep'ed for in the processes list and tries to kill #the processes involved use strict; chomp(my $hostname = `hostname`); chomp(my $username = `whoami`); my $pattern = shift; check_for_entity ("$pattern"); sub check_for_entity { my $pattern = shift; # we first do a grep for all but the last character of the pattern chop(my $subpattern = $pattern); my @ buffer = `ps -ef | grep $subpattern `; # print "checking for $username owned $subpattern\n"; my $line = undef; my $found = 0; my @ pids = (); foreach $line (@buffer) { if ($line =~ /$username\s+(\d+)\s+(.*)$pattern/) { if ($line =~ /generic-stop/) { # we dont like to kill ourselves do nothing } else { $found = 1; @pids = ($1, @pids); } } } if ($found > 0) { foreach my $pid (@pids) { print "Shutting down $pattern: kill $pid \n"; system ("kill -9 $pid"); } sleep 2; } }