home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / unix / question / 9352 < prev    next >
Encoding:
Text File  |  1992-07-23  |  3.2 KB  |  146 lines

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!sun-barr!ames!nsisrv!kong!arensb
  3. From: arensb@kong.gsfc.nasa.gov (Andrew Arensburger - RMS)
  4. Subject: Re: Script for killing mutiple processes?
  5. Message-ID: <1992Jul23.164838.27704@kong.gsfc.nasa.gov>
  6. Organization: Goddard Space Flight Center
  7. References: <1992Jul21.135406.10224@ncsu.edu>
  8. Date: Thu, 23 Jul 92 16:48:38 GMT
  9. Lines: 136
  10.  
  11. odkahn@eos.ncsu.edu (Opher D. Kahn) writes:
  12. >I am working with a experimental database server that forks
  13. >off child processes to serve multiple clients.  However, some
  14. >times these processes do not terminate correctly, and I am left
  15. >with 10-15 child processes that need to be killed.  Is it possible
  16. >to somehow pipe the process numbers from 'ps -aux | grep dood' into
  17. >a kill command, so that I don't have to kill each one of them
  18. >manually???  (dood is the name of the process).
  19.  
  20.     Since everybody else is jumping on this particular bandwagon,
  21. I might as well post my own 'pskill' script.
  22.  
  23. ----- begin included file ----------------------------------------
  24. #!/usr/local/bin/perl
  25. # Kill processes that match a pattern.
  26. # Usage: pskill [options] pattern
  27. # Options:
  28. #    -i    Ask for confirmation before killing. 'y', 'Y' or <return>
  29. #        will kill the process. 'q' or 'Q' will exit back to the
  30. #        shell. Anything else will leave the process alive and go on
  31. #        to the next one.
  32. #    -v    Verbose
  33. #    -<num>    Use 'kill -<num>'.
  34. #    -<NAME>    Use 'kill -<NAME>', e.g., "pskill -HUP"
  35. #    -    This isn't really an option, but rather marks the end of
  36. #        the options. This is to let you have patterns that start
  37. #        with a '-'.
  38.  
  39. $| = 1;
  40. $ask = 0;
  41. $verbose = 0;
  42. $signum = 15;
  43. %signames = (
  44.     "-HUP",     1,
  45.     "-INT",     2,
  46.     "-QUIT",     3,
  47.     "-ILL",     4,
  48.     "-TRAP",     5,
  49.     "-IOT",     6,
  50.     "-EMT",     7,
  51.     "-FPE",     8,
  52.     "-KILL",     9,
  53.     "-BUS",    10,
  54.     "-SEGV",    11,
  55.     "-SYS",    12,
  56.     "-PIPE",    13,
  57.     "-ALRM",    14,
  58.     "-TERM",    15,
  59.     "-URG",    16,
  60.     "-STOP",    17,
  61.     "-TSTP",    18,
  62.     "-CONT",    19,
  63.     "-CHLD",    20,
  64.     "-TTIN",    21,
  65.     "-TTOU",    22,
  66.     "-IO",    23,
  67.     "-XCPU",    24,
  68.     "-XFSZ",    25,
  69.     "-VTALRM",26,
  70.     "-PROF",    27,
  71.     "-WINCH",28,
  72.     "-LOST",    29,
  73.     "-USR1",    30,
  74.     "-USR2",    31
  75. );
  76. parseargs: while (($arg = shift(@ARGV)) =~ /^-/)
  77. {
  78.     switch: {
  79.         if ($arg eq "-i")
  80.         {
  81.             $ask = 1;
  82.             last switch;
  83.         }
  84.  
  85.         if ($arg eq "-v")
  86.         {
  87.             $verbose = 1;
  88.             last switch;
  89.         }
  90.  
  91.         if ($arg =~ /^-([0-9]+)/)
  92.         {
  93.             $signum = $1;
  94.             last switch;
  95.         }
  96.  
  97.         # Symbolic signal name
  98.         if (defined($signames{$arg}))
  99.         {
  100.             $signum = $signames{$arg};
  101.             last switch;
  102.         }
  103.  
  104.         if ($arg eq "-")
  105.         {
  106.             $arg = shift(@ARGV);
  107.             last parseargs;
  108.         }
  109.  
  110.         print STDERR "Warning: unrecognized option: $arg . Ignored\n";
  111.     }
  112. }
  113. $pat = $arg;
  114. die("No pattern specified.\nStopped") if $pat eq '';
  115.  
  116. open(PS,"ps -auwx|") || die("AArgh!");
  117. $_ = <PS>;        # Eat the first line of cruft
  118. while (<PS>)
  119. {
  120.     next if /^\S+\s+$$/;
  121.     next unless /$pat/;
  122.     ($userid,$pid) = split(/\s+/);
  123.     print if $verbose && !$ask;
  124.     if (!$ask || &askabout($_))
  125.     {
  126.         print "kill -$signum $pid\n";
  127.         kill $signum, $pid || die("kill: $!\nStopped");
  128.     }
  129. }
  130.  
  131. sub askabout
  132. {
  133.     local ($str) = @_;
  134.     local ($c);
  135.  
  136.     print "$str ?\n";
  137.     $c = <>;
  138.     exit(1) if $c eq EOF || $c =~ /^[qQ]/;
  139.     return($c =~ /^[yY\n]/);
  140. }
  141. ----- end included file ------------------------------------------
  142. --
  143. Andrew Arensburger            | Practice color discrimination:
  144. arensb@kong.gsfc.nasa.gov     | stop at red lights!
  145. ...!uunet!dftsrv!kong!arensb  | 
  146.