home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.shell
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!destroyer!ubc-cs!dhami
- From: dhami@cs.ubc.ca (Mandeep S Dhami)
- Subject: > Need Script To Kill Process Using Substring Of Application Name
- Message-ID: <1992Aug31.013546.16535@cs.ubc.ca>
- Summary: from lwall
- Keywords: zap, perl
- Sender: usenet@cs.ubc.ca (Usenet News)
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Date: Mon, 31 Aug 92 01:35:46 GMT
- Lines: 93
-
- From westes@netcom.com:
- >
- > Does anyone have a script that they could send to me that will take as
- > its input some substring and then kill any process you own that has that
- > substring in its application name? So the script would do something
- > like:
- >
- > ps guaxww | grep "some_substring"
- >
- > then it would exclude the line that contained the grep command, locate
- > the PID of the remaining processes, and kill each one. Please post the
- > script for others to see if its short enough.
- >
- I use this perl script (from THE CAMEL BOOK), hope it helps:
- -------------------8<----- snip ----->8-------------------------
- #!/usr/local/bin/perl
-
- # Usage: zap [-signal] pattern
-
- # Configuration parameters.
-
- $sig = 'TERM';
-
- $BSD = -f '/vmunix';
- $pscmd = $BSD ? "ps -auxww" : "ps -ef";
-
- open(TTYIN, "</dev/tty") || die "can't read /dev/tty: $!";
- open(TTYOUT, ">/dev/tty") || die "can't write /dev/tty: $!";
- select(TTYOUT);
- $| = 1;
- select(STDOUT);
- $SIG{'INT'} = 'cleanup';
-
- if ($#ARGV >= $[ && $ARGV[0] =~ /^-/) {
- if ($ARGV[0] =~ /-(\w+)$/) {
- $sig = $1;
- } else {
- print STDERR "$0: illegal argument $ARGV[0] ignored\n";
- }
- shift;
- }
-
- if ($BSD) {
- system "stty cbreak </dev/tty >/dev/tty 2>&1";
- }
- else {
- system "stty", 'cbreak',
- system "stty", 'eol', '^A';
- }
-
- open(PS, "$pscmd|") || die "can't run $pscmd: $!";
- $title = <PS>;
- print TTYOUT $title;
-
- # Catch any errors with eval. A bad pattern, for instance.
-
- eval <<'EOF';
- while ($cand = <PS>) {
- chop($cand);
- ($user, $pid) = split(' ', $cand);
- next if $pid == $$;
- $found = !@ARGV;
- foreach $pat (@ARGV) {
- $found = 1 if $cand =~ $pat;
- }
- next if (!$found);
- print TTYOUT "$cand? ";
- read(TTYIN, $ans, 1);
- print TTYOUT "\n" if ($ans ne "\n");
- if ($ans =~ /^y/i) { kill $sig, $pid; }
- if ($ans =~ /^q/i) { last; }
- }
- EOF
- &cleanup;
-
- sub cleanup {
- if ($BSD) {
- system "stty -cbreak </dev/tty >/dev/tty 2>&1";
- }
- else {
- system "stty", 'icanon';
- system "stty", 'eol', '^@';
- }
- print "\n";
- exit;
- }
-
- # End of file
- --
- __________________________________________________________________
- "I'm very brave generally," he went on in a low voice:
- "only to-day I happen to have a headache."
- -- Lewis Carroll
-