#!/usr/local/bin/perl # find processes that match name specified by first parameter if second parameter # is correctly formatted in seconds only processes matching passed name and number # of seconds old or older will be killed. # # Kevin P. Inscoe (kevin@inscoe.org) October 14, 2002 # # use Proc::ProcessTable; use Time::localtime; $ref = new Proc::ProcessTable; # Check parameters for required if ( ( @ARGV < 2 ) || ( $ARGV[1] == 0 ) ) { print "ageoff.pl usage:\n"; print "\n"; print "$0 match-name seconds-old-before-kill (integer)\n"; print "\n"; exit 0; } $killtime = time-$ARGV[1]; foreach $proc (@{$ref->table}) { next unless ( $proc->{cmndline} =~ m/$ARGV[0]/ ) && ( $proc->{cmndline} !~ m/$0/ ); # print "pid=$proc->{pid} start=$proc->{start} cmd=$proc->{cmndline}\n"; if ( $proc->{start} < $killtime ) { $xtime=ctime($proc->{start}); print "Killing pid $proc->{pid} start=($xtime) $proc->{cmndline}...\n"; kill 9, $proc->{pid}; } }