[comp.unix.questions] killbyname and killpid

merlyn@iwarp.intel.com (Randal Schwartz) (02/14/90)

In article <131696@sun.Eng.Sun.COM>, rock%warp (Bill Petro (SunOS Marketing)) writes:
| 
| I don't know if this is appropriate, but here is a handy little tool I
| use:
[tool deleted]

Well, here's a *couple* of tools I use to 'killbyname' and 'killpid'.
The first requires the second.

################################################## begin killbyname
#!/usr/bin/perl

$target = shift;

@pids = grep(s/^\S+\s+(\S+).*\s($target)$/$1/, split(/\n/,`ps uaxcww`));

exec 'killpid', @pids;

die "Cannot exec killpid ($!)";
################################################## end killbyname

################################################## begin killpid
#!/usr/bin/perl

for $a (@ARGV) {
	print "killing $a...\n";
	(print "$a already dead\n"), next unless kill 0, $a;
	kill 1, $a;
	sleep 1;
	next unless kill 0, $a;
	print "$a is stubborn, trying SIGKILL...\n";
	kill 9, $a;
	sleep 1;
	next unless kill 0, $a;
	print "$a won't die!\n";
}

################################################## end killpid

Usage is basically "killbyname sendmail" (to kill all sendmail
processes).  "killpid" was invented because I was tired of typing
"kill 12345", waiting to see if it died, and then "kill -9 12345".

Just another Perl hacker,
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/