From owner-freebsd-hackers Fri May 23 12:46:01 1997 Return-Path: Received: (from root@localhost) by hub.freebsd.org (8.8.5/8.8.5) id MAA11535 for hackers-outgoing; Fri, 23 May 1997 12:46:01 -0700 (PDT) Received: from regina.ibs-us.net (regina.ibs-us.net [208.131.3.35]) by hub.freebsd.org (8.8.5/8.8.5) with ESMTP id MAA11527 for ; Fri, 23 May 1997 12:45:55 -0700 (PDT) Received: (from fisbis@localhost) by regina.ibs-us.net (8.7.4/8.7.3) id MAA25882; Fri, 23 May 1997 12:48:28 -0700 (PDT) Date: Fri, 23 May 1997 12:48:27 -0700 (PDT) From: "derekb's mlist" To: freebsd-hackers@FreeBSD.ORG Subject: Re: process monitoring tool In-Reply-To: <14239.199705231334@bourbon.csv.warwick.ac.uk> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-hackers@FreeBSD.ORG X-Loop: FreeBSD.org Precedence: bulk On Fri, 23 May 1997, Mr M P Searle wrote: > > > >> It struck me as a rather nice idea. No more "ps | grep sendmail ... > > > >> kill ... sendmail -bd -q1h" - just do "nanny restart sendmail". I use a little script 'pskill'. to restart sendmail I would do "pskill sendmail HUP". It has some more practical interactive applications as well. for whom who are courious: #!/usr/bin/perl # pskill # # kills a process by `ps |` # # wr: derekb@teleport.com '96 ############################################### GLOBAL: { # is this solaris ? $SOLARIS = 0; # external executables used $PS = 'ps'; $KILL = 'kill'; # what to skip over @MYSELF = ('ps -','pskill'); # how to call ps $PS .= $SOLARIS ? ' -e -o "pid user args"' : ' -ax -o "pid user command"'; } &main(@ARGV); ############################################### sub usage { print " pskill usage: pskill [regexp] [kill_option] pskill uses ps to go through processs. If no options are used then pskill will go through all processes interactivly. If only regexp is used, pskill will filter for the regexp and iterate through these processes interactively. If regexp AND kill_option is used, pskill will be non-interactive. pskill will execute `kill -kill_option PID` for the processes that match regexp. EXAMPLES: This will iterate through all of the processes owned by user ben. pskill ben This will HUP the root processes of httpd with no interaction pskill 'root.*httpd' -HUP "; exit; } ############################################### sub dokill { # kill pid open KILL, "@_ |" or die "cannot run kill : $!"; print while(); close KILL; } ############################################### sub whatodo { # prompt user for action my $r; print "--> kill ? (N,n,y,ARG) [n]"; chop($r = ); $r; } ############################################### sub killthis { # non-interactive kill my ($p,$h) = @_; $h =~ s/^-//; &dokill($KILL,"-$h",$p); } ############################################### ############### MAIN sub main { # get argv $kill_what = shift; $kill_how = shift; # sho usage ? &usage if ($kill_what eq "-h"); # start ps open P,"$PS |" or die "cannot run ps : $!"; PROC : while (

) { ($pid, $_) = /^\s*(\d+)\s+(.+)$/ or next; if ($kill_what) { next PROC unless (/$kill_what/) } foreach $me (@MYSELF) { next PROC if (/$me/) } if ($kill_how) { # noninteractive &killthis($pid, $kill_how); next PROC; } else { # interactive print "\n$_\n"; for (&whatodo) { next PROC unless ($_); next PROC if (/^n/); last PROC if (/^N/); &dokill($KILL, $pid ), next if (/^y/i); s/^-//; &dokill($KILL, "-$_" , $pid); } } } close P; }