home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / unix / question / 9288 < prev    next >
Encoding:
Internet Message Format  |  1992-07-22  |  3.4 KB

  1. Path: sparky!uunet!mcsun!Germany.EU.net!unido!pki-nbg!hitkw14!smr
  2. From: smr@iti.org (Stephen Riehm)
  3. Newsgroups: comp.unix.questions
  4. Subject: Re: Script for killing mutiple processes?
  5. Message-ID: <smr.711814165@hitkw14>
  6. Date: 22 Jul 92 14:09:25 GMT
  7. References: <1992Jul21.135406.10224@ncsu.edu>
  8. Sender: news@pki-nbg.philips.de
  9. Lines: 111
  10.  
  11. odkahn@eos.ncsu.edu (Opher D. Kahn) writes:
  12.  
  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.  
  18. for a whlie I have had the following script lying around.. it might be
  19. of some help to you... ( OK its csh, but I wrote it before I got to
  20. like /bin/sh and I can't be bothered converting it blah blah blah )
  21.  
  22. Some notes:
  23.     There is no warrantee, it will always prompt, if you kill jobs
  24.         its your problem!
  25.     The default answer is always YES, so just hitting return WILL
  26.         kill the prompted job. Some people don't like this, I do ;-)
  27.     I don't use ps to just list the command name, for the simple
  28.         reason that this can be used to kill a particular vi session
  29.         that got hung which editing a given file ( believe me this
  30.         is possible! ) ( ie: slay filename )
  31.     This is for use with sysv style machines at the moment, I
  32.         believe the right options for bsd are ps -glx, but you will
  33.         have to change all the cuts etc etc
  34.  
  35. catchya
  36.  
  37. -----------------------------------------------------------------
  38. Stephen Riehm                   smr@pki-nbg.philips.de
  39. not in any way talking on behalf of:
  40. Philips Kommunikations Industrie      Phone: +49 911 526 2975
  41. 8500 Nuremberg, Germany                Fax: +49 911 526 2095
  42. I come from the land down under, where women glow and men chunder
  43.  
  44. --- cut here --- file: slay
  45. [nosave]
  46. #!/bin/csh -f
  47. #
  48. # cshell script to kill jobs of a particular name
  49. #
  50. # Usage:    slay <jobname>...
  51. #
  52. # Written By:    Stephen Riehm        ( smr@pki-nbg.philips.de )
  53. #
  54. # Permission to copy and distribute this file is granted, see the GNU
  55. # Free Software Copyright file for details.
  56. #
  57. #
  58. # if no jobs specified, try to remove defunct jobs if possible
  59. #
  60. if ( $#argv == 0 ) then
  61.     set argv="defunct"
  62. endif
  63.  
  64. foreach i ( $argv )
  65.  
  66. #
  67. # find all the jobs owned by the current user with $i in their command string
  68. #
  69.     foreach j ( `ps -u $LOGNAME | grep $i | grep -v grep | cut -c1-7` )
  70.     set js=`ps -fp $j | grep -v PPID`
  71.     set jd=`echo $js | cut -d'<' -f2`
  72.  
  73. #
  74. # get the parent ID of defunct processes
  75. #
  76.     if ( "$jd" == "defunct>" ) then
  77.         set p=`echo $js | cut -c16-20`
  78.         set jp=`ps -fp $p | grep -v PPID`
  79.         echo $jp
  80.         echo -n $j is defunct, kill it's parent $p ' ? [y]'
  81.     else
  82.         echo $js
  83.         echo -n kill $j '? [y]'
  84.     endif
  85.     set q=$<
  86.     if ( $q == "n" || $q == "no" ) then
  87.         echo $j left to run free
  88.     else if ( $q == "q" ) then
  89. #
  90. # short cut if you don't want to kill any jobs after all
  91. # ( undocumented feature... best kind! )
  92. #
  93.         exit
  94.     else
  95. #
  96. # try a nice kill first
  97. #
  98.         ( kill $j )
  99. #
  100. # wait for something to happen ( you have 1 second to pack up and leave )
  101. #
  102.         sleep 1
  103. #
  104. # if the process still exists, ask for permission to go 'ALL OUT'
  105. #
  106.         if ( `ps -p $j | wc -l` != "1"  ) then
  107.         echo -n "$j didn't die... shall I nuke it?? [y]"
  108.         set q=$<
  109.         if( $q == "n" ) then
  110.             echo ""
  111.             echo "O.K. have it your way!"
  112.         else
  113.             ( kill -9 $j )
  114.             echo $j killed
  115.         endif
  116.         else
  117.         echo $j killed
  118.         endif
  119.     endif
  120.     end
  121. end
  122.