home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / unix / shell / 3702 < prev    next >
Encoding:
Text File  |  1992-08-30  |  2.7 KB  |  106 lines

  1. Newsgroups: comp.unix.shell
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!destroyer!ubc-cs!dhami
  3. From: dhami@cs.ubc.ca (Mandeep S Dhami)
  4. Subject: > Need Script To Kill Process Using Substring Of Application Name
  5. Message-ID: <1992Aug31.013546.16535@cs.ubc.ca>
  6. Summary: from lwall
  7. Keywords: zap, perl
  8. Sender: usenet@cs.ubc.ca (Usenet News)
  9. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  10. Date: Mon, 31 Aug 92 01:35:46 GMT
  11. Lines: 93
  12.  
  13. From westes@netcom.com:
  14. >
  15. > Does anyone have a script that they could send to me that will take as
  16. > its input some substring and then kill any process you own that has that
  17. > substring in its application name?  So the script would do something
  18. > like:
  19. >     ps guaxww | grep "some_substring"
  20. > then it would exclude the line that contained the grep command, locate
  21. > the PID of the remaining processes, and kill each one.  Please post the
  22. > script for others to see if its short enough.
  23. I use this perl script (from THE CAMEL BOOK), hope it helps:
  24. -------------------8<----- snip ----->8-------------------------
  25. #!/usr/local/bin/perl
  26.  
  27. # Usage: zap [-signal] pattern
  28.  
  29. # Configuration parameters.
  30.  
  31. $sig = 'TERM';
  32.  
  33. $BSD = -f '/vmunix';
  34. $pscmd = $BSD ? "ps -auxww" : "ps -ef";
  35.  
  36. open(TTYIN, "</dev/tty") || die "can't read /dev/tty: $!";
  37. open(TTYOUT, ">/dev/tty") || die "can't write /dev/tty: $!";
  38. select(TTYOUT);
  39. $| = 1;
  40. select(STDOUT);
  41. $SIG{'INT'} = 'cleanup';
  42.  
  43. if ($#ARGV >= $[ && $ARGV[0] =~ /^-/) {
  44.     if ($ARGV[0] =~ /-(\w+)$/) {
  45.     $sig = $1;
  46.     } else {
  47.     print STDERR "$0: illegal argument $ARGV[0] ignored\n";
  48.     }
  49.     shift;
  50. }
  51.  
  52. if ($BSD) {
  53.     system "stty cbreak </dev/tty >/dev/tty 2>&1";
  54. }
  55. else {
  56.     system "stty", 'cbreak',
  57.     system "stty", 'eol', '^A';
  58. }
  59.  
  60. open(PS, "$pscmd|") || die "can't run $pscmd: $!";
  61. $title = <PS>;
  62. print TTYOUT $title;
  63.  
  64. # Catch any errors with eval.  A bad pattern, for instance.
  65.  
  66. eval <<'EOF';
  67. while ($cand = <PS>) {
  68.     chop($cand);
  69.     ($user, $pid) = split(' ', $cand);
  70.     next if $pid == $$;
  71.     $found = !@ARGV;
  72.     foreach $pat (@ARGV) {
  73.     $found = 1 if $cand =~ $pat;
  74.     }
  75.     next if (!$found);
  76.     print TTYOUT "$cand? ";
  77.     read(TTYIN, $ans, 1);
  78.     print TTYOUT "\n" if ($ans ne "\n");
  79.     if ($ans =~ /^y/i) { kill $sig, $pid; }
  80.     if ($ans =~ /^q/i) { last; }
  81. }
  82. EOF
  83. &cleanup;
  84.  
  85. sub cleanup {
  86.     if ($BSD) {
  87.     system "stty -cbreak </dev/tty >/dev/tty 2>&1";
  88.     }
  89.     else {
  90.     system "stty", 'icanon';
  91.     system "stty", 'eol', '^@';
  92.     }
  93.     print "\n";
  94.     exit;
  95. }
  96.  
  97. # End of file
  98. --
  99. __________________________________________________________________
  100. "I'm very brave generally," he went on in a low voice:
  101. "only to-day I happen to have a headache."
  102.                                       -- Lewis Carroll
  103.