#!/usr/bin/perl -w # turn on strict forcing definition of variables and subs/functions use strict; # FOR UBUNTU ONLY ****************** # Declare Variables my @proc = ""; # Array to hold processes to search for and stop my @result_top = ""; my @Search_array = ""; # This is what will be searched for. A line # that matches any of the substrings contained in the # Search_Array will be filetered out. my $line = "" ; # line of input file my $len = 0; # Used for getting length of a string. my $getresult = -1; # -1 will denote not found after searching for # a substring within a string. # Declare Function/Sub Routines sub Searchme # Returns -1 if Substring not found in input line # Otherwise it returns a number of zero or greater. { my $j = 0; #iteration loop scalar counter my $result = -1; # used for checking for substring # A "-1" denotes not found. # Given an input line search for elements in the Search_array. my ($inline, @Search2_array) = @_; my $knum = 0; #Set and define default number of Substrings to search. $knum = scalar@Search2_array; #Get number of elements (substrings) # to search line for. # Note we must subtract -1 from $knum so we do not go past the # end of the search array. for ( $j = 0; $j <= ($knum -1 ); $j++) { $result = index($inline, $Search2_array[$j]); # print " This is search array element <$Search2_array[$j]> \n"; # A value of -1 is returned for not found. if ($result >= 0) {return $result; next;} # Exit prematurely found a substring # that matches. } return $result; # if we get here no matching substrings. } # End sub Searchme # End Subroutine Function Section # # Begin Main Program Section ################################### # # Usage: perlstopproc proc1 proc2 proc3 # proc1 proc2 proc3 will be the name of the processes to stop. # i.e. ./perlstopproc HPScanjet GLTerm my $count_procs = scalar@ARGV; if ($count_procs < 1 ) { print " You must have at least one process to stop \n"; print " Usage: ./perlstopproc proc1 proc2 proc3 etc. \n"; print " Note, This is a very powerful program to be used \n"; print " extremely cautiously. proc1 is not necessarily the \n"; print " full name of the process. The letters HP will be \n"; print " enought to stop all the processes that start with these \n"; print " two letters, such as HPScanjet , HPprinter etc. \n"; print " Do not use this perl script as a root user! \n"; print " Otherwise, you could take down your whole system! \n"; exit;} # no processes to stop # print " parameter count = $count_procs \n"; my $j = 0; while ($count_procs > 0) { $proc[$j] = pop(@ARGV); push @Search_array, $proc[$j] ; $count_procs--; $j++; } # Note above algorithm because of loops puts an extra empty element # in array. Do the following to remove it. shift(@Search_array);# to remove The empty element introduced. @Search_array = reverse(@Search_array); print "This is pattern Search_array for processes to stop: <@Search_array> \n"; # Display first 40 entries and save it as a result_top array to search later # for process to stop. # UNIX Command on MAC top -u -l1 40 # Linux Command on Ubuntu top -b -n 1 # Note, the above will get the same information as the MAC using 1 iteration.The # -b is for batch mode to dump the result to a file or array. In Ubuntu you automatically # get the entire memory list of processess. On the MAC, I had it grab the top 40 processes. # Using Ubuntu here********************* @result_top = `top -b -n 1` ; # ************************************** # print "this is the result: @result_top \n"; my $i = 0; #counter for @result_top array foreach (@result_top) # Keep Reading array to end { $getresult = Searchme($result_top[$i], @Search_array); if ($getresult >= 0 ) { # For Ubuntu ************************************************* my $pid = substr($result_top[$i], 0, 5); # On Ubuntu the the first 6 characters will be the PID number. print "stoping this process $pid \n"; my $stop_proc = ` kill -9 $pid `; } # Kill this process. #end of for loop block $i++; } # End Main Section ************************** exit;