Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 23 May 1997 12:48:27 -0700 (PDT)
From:      "derekb's mlist" <fisbis@ibs-us.net>
To:        freebsd-hackers@FreeBSD.ORG
Subject:   Re: process monitoring tool
Message-ID:  <Pine.SOL.3.91.970523123949.15877B@regina>
In-Reply-To: <14239.199705231334@bourbon.csv.warwick.ac.uk>

next in thread | previous in thread | raw e-mail | index | archive | help
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(<KILL>); close KILL;
}
###############################################
sub whatodo {                      # prompt user for action
    my $r;
    print "--> kill ? (N,n,y,ARG) [n]"; chop($r = <STDIN>); 
    $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 (<P>) {
       
	($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;
}







Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.SOL.3.91.970523123949.15877B>