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

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!elroy.jpl.nasa.gov!usc!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!cbnewsj!btwomey
  3. From: btwomey@cbnewsj.cb.att.com (william.tw0mey)
  4. Subject: Re: Script for killing mutiple processes?
  5. Organization: AT&T
  6. Date: Wed, 22 Jul 1992 15:24:11 GMT
  7. Message-ID: <1992Jul22.152411.16083@cbnewsj.cb.att.com>
  8. References: <1992Jul21.135406.10224@ncsu.edu> <1992Jul21.173924.14654@bnr.uk>
  9. Lines: 37
  10.  
  11. In article <1992Jul21.173924.14654@bnr.uk> agc@bnr.ca (Alan Carter) writes:
  12. >In article <1992Jul21.135406.10224@ncsu.edu>, odkahn@eos.ncsu.edu (Opher D. Kahn) writes:
  13. >|> with 10-15 child processes that need to be killed.  Is it possible
  14. >|> to somehow pipe the process numbers from 'ps -aux | grep dood' into
  15. >|> a kill command, so that I don't have to kill each one of them
  16. >|> manually???  (dood is the name of the process).
  17. >|> Thanks, 
  18. >
  19. >There is a general magic for this kind of thing. Bear in mind I've got System V
  20. >type ps here, and Korn shell. 
  21. >
  22.  
  23. >      #!/bin/ksh
  24. >      ps -ef | grep dood | grep -v grep | awk { print $2 } | while read var
  25. >      do
  26. >         echo "THIS IS DEATH, $var"
  27. >         kill $var
  28. >      done | more
  29.  
  30. A little better:
  31. ps -ef | awk '/awk/ { next }
  32. /dood/ { print $2} ' | xargs kill
  33.  
  34. The advantage is that you have shorter pipeline to execute, 
  35. as well as understand.  In an unrigorous test, ps|grep|grep|cut 
  36. used more real time and system time than ps|awk on an HP 9000/855.
  37.  
  38. When killing a process that has children, grandchildren, ...,
  39. I try to kill the youngest children first.
  40. If pids have not cycled back to 0+, pipe the output of awk through
  41. sort -n -r +1 -2, then to xargs.
  42. Handling the case where pids have wrapped, is left as an exercise
  43. to the reader.
  44.  
  45. Bill Twomey
  46. -- 
  47. bt@violin.att.com
  48.