#!/usr/bin/perl -w # (c) 2001 Thomas Guettler http://www.thomas-guettler.de # This script is in the public domain # Feedback welcome # # Kill processes matching a regular expression # Simple alternative to pkill of package procps # TODO: # -Use perl function to access process managment instead of @ps=`ps ...` my $force=0; my $print_only=0; my $regex; if($#ARGV==-1 || $ARGV[0]=~/-h/){ print "Usage: $0 [-p|-f] [num] regex\n" . " Kill processes matching a regular expression\n" . " -p: print processes matching regular-expression, don't kill\n" . " -f: force (don't ask)\n" . " num: send specified number to process. 15 if none given\n"; exit; } if($ARGV[0]=~/^-p/){ $print_only=1; shift(@ARGV); } elsif($ARGV[0]=~/^-f/){ $force=1; shift(@ARGV); } $signal=15; if($ARGV[0]=~/^\d*$/){ $signal=$ARGV[0]; shift(@ARGV); } $regex=$ARGV[0]; @ps=`ps --no-header -Ao pid,args`; $kill_apps="kill -$signal "; $num2kill=0; foreach $ps (@ps){ $ps =~ /^\s*(\d*)\s*(.*)$/; $pid=$1; # process-ID $command=$2; if($command=~/$regex/ && $$!=$pid){ if($print_only or !$force){ print "$pid $command\n"; } $kill_apps.="$pid "; $num2kill++; } } if(!$print_only && $num2kill!=0){ $answer="y"; if($force==0){ print "Kill theses processes? (y|n)"; $answer=; } if($answer=~/^y/ or $answer=~/^Y/){ print $kill_apps; print `$kill_apps 2>&1`; print "\n"; } else{ print "No processes killed\n"; } }