Xref: utzoo comp.unix.questions:19876 comp.lang.perl:365 Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!swrinde!ucsd!ogicse!littlei!omepd!iwarp.intel.com!news From: merlyn@iwarp.intel.com (Randal Schwartz) Newsgroups: comp.unix.questions,comp.lang.perl Subject: killbyname and killpid (perl) (was Re: Killing the correct process) Message-ID: <1990Feb13.174545.4644@iwarp.intel.com> Date: 13 Feb 90 17:45:45 GMT References: <22332@adm.BRL.MIL> <5312@star.cs.vu.nl> <1212@root44.co.uk> <5352@star.cs.vu.nl> <131696@sun.Eng.Sun.COM> Sender: news@iwarp.intel.com Reply-To: merlyn@iwarp.intel.com (Randal Schwartz) Organization: Stonehenge; netaccess via Intel, Beaverton, Oregon, USA Lines: 49 In-Reply-To: rock%warp@Sun.COM (Bill Petro (SunOS Marketing)) 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!"=/