home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.questions
- Path: sparky!uunet!sun-barr!ames!nsisrv!kong!arensb
- From: arensb@kong.gsfc.nasa.gov (Andrew Arensburger - RMS)
- Subject: Re: Script for killing mutiple processes?
- Message-ID: <1992Jul23.164838.27704@kong.gsfc.nasa.gov>
- Organization: Goddard Space Flight Center
- References: <1992Jul21.135406.10224@ncsu.edu>
- Date: Thu, 23 Jul 92 16:48:38 GMT
- Lines: 136
-
- odkahn@eos.ncsu.edu (Opher D. Kahn) writes:
- >I am working with a experimental database server that forks
- >off child processes to serve multiple clients. However, some
- >times these processes do not terminate correctly, and I am left
- >with 10-15 child processes that need to be killed. Is it possible
- >to somehow pipe the process numbers from 'ps -aux | grep dood' into
- >a kill command, so that I don't have to kill each one of them
- >manually??? (dood is the name of the process).
-
- Since everybody else is jumping on this particular bandwagon,
- I might as well post my own 'pskill' script.
-
- ----- begin included file ----------------------------------------
- #!/usr/local/bin/perl
- # Kill processes that match a pattern.
- # Usage: pskill [options] pattern
- # Options:
- # -i Ask for confirmation before killing. 'y', 'Y' or <return>
- # will kill the process. 'q' or 'Q' will exit back to the
- # shell. Anything else will leave the process alive and go on
- # to the next one.
- # -v Verbose
- # -<num> Use 'kill -<num>'.
- # -<NAME> Use 'kill -<NAME>', e.g., "pskill -HUP"
- # - This isn't really an option, but rather marks the end of
- # the options. This is to let you have patterns that start
- # with a '-'.
-
- $| = 1;
- $ask = 0;
- $verbose = 0;
- $signum = 15;
- %signames = (
- "-HUP", 1,
- "-INT", 2,
- "-QUIT", 3,
- "-ILL", 4,
- "-TRAP", 5,
- "-IOT", 6,
- "-EMT", 7,
- "-FPE", 8,
- "-KILL", 9,
- "-BUS", 10,
- "-SEGV", 11,
- "-SYS", 12,
- "-PIPE", 13,
- "-ALRM", 14,
- "-TERM", 15,
- "-URG", 16,
- "-STOP", 17,
- "-TSTP", 18,
- "-CONT", 19,
- "-CHLD", 20,
- "-TTIN", 21,
- "-TTOU", 22,
- "-IO", 23,
- "-XCPU", 24,
- "-XFSZ", 25,
- "-VTALRM",26,
- "-PROF", 27,
- "-WINCH",28,
- "-LOST", 29,
- "-USR1", 30,
- "-USR2", 31
- );
- parseargs: while (($arg = shift(@ARGV)) =~ /^-/)
- {
- switch: {
- if ($arg eq "-i")
- {
- $ask = 1;
- last switch;
- }
-
- if ($arg eq "-v")
- {
- $verbose = 1;
- last switch;
- }
-
- if ($arg =~ /^-([0-9]+)/)
- {
- $signum = $1;
- last switch;
- }
-
- # Symbolic signal name
- if (defined($signames{$arg}))
- {
- $signum = $signames{$arg};
- last switch;
- }
-
- if ($arg eq "-")
- {
- $arg = shift(@ARGV);
- last parseargs;
- }
-
- print STDERR "Warning: unrecognized option: $arg . Ignored\n";
- }
- }
- $pat = $arg;
- die("No pattern specified.\nStopped") if $pat eq '';
-
- open(PS,"ps -auwx|") || die("AArgh!");
- $_ = <PS>; # Eat the first line of cruft
- while (<PS>)
- {
- next if /^\S+\s+$$/;
- next unless /$pat/;
- ($userid,$pid) = split(/\s+/);
- print if $verbose && !$ask;
- if (!$ask || &askabout($_))
- {
- print "kill -$signum $pid\n";
- kill $signum, $pid || die("kill: $!\nStopped");
- }
- }
-
- sub askabout
- {
- local ($str) = @_;
- local ($c);
-
- print "$str ?\n";
- $c = <>;
- exit(1) if $c eq EOF || $c =~ /^[qQ]/;
- return($c =~ /^[yY\n]/);
- }
- ----- end included file ------------------------------------------
- --
- Andrew Arensburger | Practice color discrimination:
- arensb@kong.gsfc.nasa.gov | stop at red lights!
- ...!uunet!dftsrv!kong!arensb |
-