#!/usr/bin/perl -w # 'killall' by Chris Angell - version 0.00002 # This script runs on OpenBSD 3.0 - can be used on almost any system with minor (if any!) modifications # This code is open source and free to all for any use. Enjoy! use strict; usage() if !@ARGV || @ARGV > 2; # need at least one arg, no more than two kill get_sig(), get_pids(); # kill process(es) sub get_sig { my $bad_signame = sub { warn "Bad signal name used: $_[0]\n" and exit(2) }; if ($ARGV[0] =~ /^-(\d{1,2})$/) { # numerical signal warn "Signal number out of bounds: $1" and exit(3) unless $1 > 0 && $1 < 32; # may vary on other systems return shift(@ARGV) } elsif ($ARGV[0] =~ /^-([A-Z]{3,5})$/) { # signal name require POSIX; my $signum; eval '$signum = POSIX::SIG' . $1 . '()' or $bad_signame->($1); # calls POSIX::SIG$1() shift(@ARGV) and return $signum; } $bad_signame->($ARGV[0]) if $ARGV[1]; # bad signal name if it got this far and there is still one arg left return 15; # default signal returned if no other provided } sub get_pids { my @ps = `/bin/ps -axc -o uid -o pid -o command`; my @need_killed; for (@ps[1..$#ps]) { # skip first line my ($uid, $pid, $command) = split; next if $pid == $$; # don't kill yourself next unless $uid == $< || 0 == $<; # next unless user owns this UID or UID is zero push @need_killed, $pid if $command eq $ARGV[0]; # match up } print "No matching processes ", (0 != $<) && "belonging to you were ", "found\n" and exit(1) unless @need_killed; return @need_killed; } sub usage { $0 =~ s!.+/!!; print "usage: $0 name / $0 -1 name / $0 -HUP name\n"; exit(-1) } __END__ =head1 NAME killall - kill a process (or processes) by name =head1 USAGE 'killall' signals all processes that match the command name and are owned by the current user. When run with a UID of 0, all processes will be killed, not only the PIDs owned by the current PID. The default signal sent is 15/HUP if no other is specified. Here are some example uses. Restart sendmail: killall -1 sendmail or killall -HUP sendmail Send SIGKILL to ftpd: killall -KILL ftpd or killall -9 ftpd Kill all instances of httpd: killall httpd =head1 AUTHOR Chris Angell =cut