Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 12 Jun 1995 21:27:55 +0200
From:      Wolfram Schneider <wosch@cs.tu-berlin.de>
To:        current@FreeBSD.ORG
Subject:   killall
Message-ID:  <199506121927.VAA15888@ole.cs.tu-berlin.de>

next in thread | raw e-mail | index | archive | help

I miss the killall program like in Linux or Irix. Here is a little
perl hack.


#!/usr/bin/perl
#
# killall - kill all processes
#
# Note: work only with effective uid due the limit of procfs
#       (eg. not with suid programs)

$ENV{'PATH'} = "/bin:/usr/bin";
$procfs = '/proc';
$signal = 'SIGTERM';		# default signal for kill
$debug = 0;

$PROC_NAME = 0 + $[;
$PROC_RUID = 11 + $[;
$PROC_REST = 12 + $[;

sub usage {
    die "killall [-v] [-?|-help] [-l] [-SIGNAL] program\n";
}

while ($_ = $ARGV[0], /^-/) {
    shift @ARGV;
    if    (/^--$/)                  { $_ = $ARGV[0]; last }
    elsif (/^-v$/)                  { $debug = 1 }
    elsif (/^-(h|help|\?)$/)        { do usage }
    elsif (/^-l$/)                  { exec 'kill', '-l' }
    elsif (/^-([A-Za-z]+|[0-9]+)$/) { $signal = $1 }
}

$program = $_;
&usage unless $program;

die "Maybe $procfs is not mounted\n" unless -e "$procfs/0/status";

while(<$procfs/[0-9]*>) {
    $file = "$_/status";
    ($pid = $_) =~ s=^$procfs/==;
    
    next if $pid == $$;		# don't kill yourself

    open(P, "$file") || next;	# process maybe already terminated
    while(<P>) {
	@proc = split;
	print "$pid $proc[$PROC_NAME] $proc[$PROC_RUID]\n" 
	    if $debug > 0;
	push(@kill, "$pid") 
	    if ($proc[$PROC_NAME] eq $program && $proc[$PROC_RUID] eq $<);
    }
    close P;
}

exit(1) if $#kill < 0;		# nothing found
$signal =~ y/[a-z]/[A-Z]/;	# signal name in upper case
print "kill $signal @kill\n" if $debug;
kill ($signal, @kill);

# end



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