home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.questions
- Path: sparky!uunet!elroy.jpl.nasa.gov!usc!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!cbnewsj!btwomey
- From: btwomey@cbnewsj.cb.att.com (william.tw0mey)
- Subject: Re: Script for killing mutiple processes?
- Organization: AT&T
- Date: Wed, 22 Jul 1992 15:24:11 GMT
- Message-ID: <1992Jul22.152411.16083@cbnewsj.cb.att.com>
- References: <1992Jul21.135406.10224@ncsu.edu> <1992Jul21.173924.14654@bnr.uk>
- Lines: 37
-
- In article <1992Jul21.173924.14654@bnr.uk> agc@bnr.ca (Alan Carter) writes:
- >In article <1992Jul21.135406.10224@ncsu.edu>, odkahn@eos.ncsu.edu (Opher D. Kahn) writes:
- >|> 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).
- >|> Thanks,
- >
- >There is a general magic for this kind of thing. Bear in mind I've got System V
- >type ps here, and Korn shell.
- >
-
- > #!/bin/ksh
- > ps -ef | grep dood | grep -v grep | awk { print $2 } | while read var
- > do
- > echo "THIS IS DEATH, $var"
- > kill $var
- > done | more
-
- A little better:
- ps -ef | awk '/awk/ { next }
- /dood/ { print $2} ' | xargs kill
-
- The advantage is that you have shorter pipeline to execute,
- as well as understand. In an unrigorous test, ps|grep|grep|cut
- used more real time and system time than ps|awk on an HP 9000/855.
-
- When killing a process that has children, grandchildren, ...,
- I try to kill the youngest children first.
- If pids have not cycled back to 0+, pipe the output of awk through
- sort -n -r +1 -2, then to xargs.
- Handling the case where pids have wrapped, is left as an exercise
- to the reader.
-
- Bill Twomey
- --
- bt@violin.att.com
-