Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 27 May 2000 09:09:06 -0700
From:      Cy Schubert - ITSD Open Systems Group <Cy.Schubert@uumail.gov.bc.ca>
To:        Brian Somers <brian@Awfulhak.org>
Cc:        John Baldwin <jhb@FreeBSD.ORG>, Randall Hopper <aa8vb@nc.rr.com>, stable@FreeBSD.ORG, housley@thehousleys.net, brian@hak.lan.Awfulhak.org
Subject:   Re: killall question 
Message-ID:  <200005271609.e4RG9cA94703@cwsys.cwsent.com>
In-Reply-To: Your message of "Sat, 27 May 2000 16:46:26 BST." <200005271546.QAA64909@hak.lan.Awfulhak.org> 

next in thread | previous in thread | raw e-mail | index | archive | help
In message <200005271546.QAA64909@hak.lan.Awfulhak.org>, Brian Somers 
writes:
> > 
> > On 27-May-00 Randall Hopper wrote:
> > > housley@thehousleys.net:
> > >  |Randall Hopper <aa8vb@nc.rr.com> said: 
> > >  |
> > >  |> I have a script I run named "newroot".  I want to kill it with killal
> l.
> > >  |>         
> > >  |>     > ps -ax | grep newroot
> > >  |>      842     1 rhh    /bin/sh /home/rhh/bin/newroot 360
> > >  |> 
> > >  |You will have to do something like
> > >  |
> > >  |kill `ps -ax | grep newroot | sed -e '^[0-9]*'`
> > > 
> > > Ok.  I thought I'd at least try to use the system version, but sounds lik
> e
> > > it's just not as flexible as killall's on other systems.
> > > 
> > > Here's the shell script I settled on to override the default /usr/bin/kil
> lall:
> > > 
> > >      ps -x | grep "$1" | egrep -v "grep|$0" | awk '{print $1;}'
> > 
> > % killall grep
> 
> # killall grepscript
> 
> There are bad sub-string implications there too....

Here's a killall script I picked up from somewhere about 6-7 years ago 
(author unknown).  I won't take credit for writing it, though I have 
modified it a little.  The advantage over the FreeBSD and Linux killall 
is that it's portable to any platform.

I've been using a version of this script for over 6 years on Solaris to 
kill a an application that won't clean up after itself properly.

#!/usr/bin/perl

# Usage: zap [-signal] pattern

# Configuration parameters.

$ENV{'PATH'}='/usr/bin:/bin:/usr/local/bin:/usr/sbin:/sbin';
$sig = 'TERM';

$BSD = -f '/vmunix' || -f '/kernel';
$pscmd = $BSD ? "ps -auxww" : "ps -ef";
$IA = 0;
$IA = 1 if ( $0 !~ /zapall/ );
$QUIET = 0;

if ($IA) {
    open(TTYIN, "</dev/tty") || die "can't read /dev/tty: $!";
    open(TTYOUT, ">/dev/tty") || die "can't write /dev/tty: $!";
    select(TTYOUT);
    $| = 1;
    select(STDOUT);
    $SIG{'INT'} = 'cleanup';
}

if ($#ARGV >= $[ && $ARGV[0] =~ /^-/) {
    if ($ARGV[0] =~ /-q$/ || $ARGV[0] =~ /-quiet$/) {
	$QUIET = 1;
    } elsif ($ARGV[0] =~ /-(\w+)$/) {
	$sig = $1;
    } else {
	print STDERR "$0: illegal argument $ARGV[0] ignored\n";
    }
    shift;
}

if ($IA) {
    if ($BSD) {
        system "stty cbreak </dev/tty >/dev/tty 2>&1";
    }
    else {
        system "stty", 'cbreak',
        system "stty", 'eol', '^A';
    }
}

open(PS, "$pscmd|") || die "can't run $pscmd: $!";
$title = <PS>;
if ($IA) {
    print TTYOUT $title;
}

# Catch any errors with eval.  A bad pattern, for instance.

eval <<'EOF';
while ($cand = <PS>) {
    chop($cand);
    ($user, $pid) = split(' ', $cand);
    next if $pid == $$;
    $found = !@ARGV;
    foreach $pat (@ARGV) {
	$found = 1 if $cand =~ $pat;
    }
    next if (!$found);
    if ($IA) {
	print TTYOUT "$cand ? ";
	read(TTYIN, $ans, 1);
	print TTYOUT "\n" if ($ans ne "\n");
	if ($ans =~ /^y/i) { kill $sig, $pid; }
	if ($ans =~ /^q/i) { last; }
    } else {
	kill $sig, $pid;
	if ($QUIET != 1) { print "Sending signal $sig to PID $pid\n"; }
    }
}
EOF
if ($IA) {
    &cleanup;
}

sub cleanup {
    if ($BSD) {
	system "stty -cbreak </dev/tty >/dev/tty 2>&1";
    }
    else {
	system "stty", 'icanon';
	system "stty", 'eol', '^@';
    }
    print "\n";
    exit;
}



Regards,                       Phone:  (250)387-8437
Cy Schubert                      Fax:  (250)387-5766
Team Leader, Sun/DEC Team   Internet:  Cy.Schubert@osg.gov.bc.ca
Open Systems Group, ITSD, ISTA
Province of BC





To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-stable" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200005271609.e4RG9cA94703>