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

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!sun-barr!cs.utexas.edu!convex!news.utdallas.edu!corpgate!bnrgate!bcars267!bmdhh243!bnr.ca!agc
  3. From: agc@bnr.ca (Alan Carter)
  4. Subject: Re: Script for killing mutiple processes?
  5. Message-ID: <1992Jul21.173924.14654@bnr.uk>
  6. Sender: news@bnr.uk (News Administrator)
  7. Nntp-Posting-Host: bmdhh298
  8. Organization: BNR-Europe-Limited, Maidenhead, England
  9. References:  <1992Jul21.135406.10224@ncsu.edu>
  10. Date: Tue, 21 Jul 1992 17:39:24 GMT
  11. Lines: 39
  12.  
  13. In article <1992Jul21.135406.10224@ncsu.edu>, odkahn@eos.ncsu.edu (Opher D. Kahn) writes:
  14. |> I am working with a experimental database server that forks
  15. |> off child processes to serve multiple clients.  However, some
  16. |> times these processes do not terminate correctly, and I am left
  17. |> with 10-15 child processes that need to be killed.  Is it possible
  18. |> to somehow pipe the process numbers from 'ps -aux | grep dood' into
  19. |> a kill command, so that I don't have to kill each one of them
  20. |> manually???  (dood is the name of the process).
  21. |> Thanks, 
  22.  
  23. There is a general magic for this kind of thing. Bear in mind I've got System V
  24. type ps here, and Korn shell. 
  25.  
  26. 1. Get the ps output, filter out the ones that mention dood, but don't forget
  27.    to throw away the line where dood is the argument!
  28.  
  29.       ps -ef | grep dood | grep -v grep 
  30.  
  31. 2. Now use a tool to chop out the pid field, I've used awk but cut would work.
  32.    Check that your Berzerkely ps puts the pid on field 2, I can't remember.
  33.  
  34.       ps -ef | grep dood | grep -v grep | awk { print $2 }
  35.  
  36. 3. Finally, do whatever you want with this stream of numbers, and chuck the 
  37.    lot through more so that you can relish the death of each. Put it in a file
  38.    with #!/bin/ksh at the top so when it is executed the correct interpreter
  39.    is used.
  40.  
  41.       #!/bin/ksh
  42.       ps -ef | grep dood | grep -v grep | awk { print $2 } | while read var
  43.       do
  44.          echo "THIS IS DEATH, $var"
  45.          kill $var
  46.       done | more
  47.  
  48. Bear in mind the dangers of letting automated kills run around on your precious
  49. system, and never, ever, do something like this as root.
  50.  
  51. Enjoy, Alan!
  52.