home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / unix / shell / 3712 < prev    next >
Encoding:
Internet Message Format  |  1992-08-31  |  4.2 KB

  1. Xref: sparky comp.unix.shell:3712 comp.unix.questions:10568
  2. Path: sparky!uunet!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!mp.cs.niu.edu!fnnews!fnsony.fnal.gov!shah
  3. From: shah@fnsony.fnal.gov (Hemant Shah)
  4. Newsgroups: comp.unix.shell,comp.unix.questions
  5. Subject: Re: Need Script To Kill Process Using Substring Of Application Name
  6. Message-ID: <2238@fnnews.fnal.gov>
  7. Date: 31 Aug 92 17:21:44 GMT
  8. References: <x6gnwvc.westes@netcom.com> <rqhnklc.tunxis@netcom.com>
  9. Sender: news@fnnews.fnal.gov
  10. Reply-To: shah@fnalb.fnal.gov
  11. Followup-To: comp.unix.shell
  12. Organization: Fermi National Accelerator Laboratory, Batavia, IL
  13. Lines: 152
  14. Nntp-Posting-Host: fnsony.fnal.gov
  15.  
  16. Here's the script that I use.
  17.  
  18. ---------- 8< ---- begin zap ---------- 8< ---------------------------
  19. #!/bin/sh
  20. #
  21. # Name: zap (zapit)
  22. # Programmer:
  23. #        Hemant Shah  (708) 840-8071
  24. #        Frank Koenen (708) 840-8042
  25. #        Fermilab
  26. #        Systems Integration Group
  27. #        March 31, 1992
  28. #
  29. # Description:
  30. # Kills processes by process name. e.g. zap xclock
  31. # It will ask for conformation before killing the process. If you do not
  32. # want it to ask for conformation then create a soft link to zapit.
  33. # e.g. ln -s zap zapit
  34. # If you type "zapit xclock", then it will not ask you for yes/no.
  35. # NOTE: This can be dangerous if used without caution.
  36. #
  37. # Also if you give more then argument on command line it will only kill the 
  38. # process that has all the arguments in the output from ps command.
  39. #
  40. # Example: There are more then one users running xterm, but you only want to
  41. # kill xterm running under username shah, then type following:
  42. # zap shah xterm
  43. #
  44. #
  45. # get the name of the program
  46. PROGNAME=`basename ${0}`
  47.  
  48. # If the program contains "it" in it e.g. zapit then do not ask for 
  49. # conformation, just kill the process.
  50. # NOTE: THIS CAN BE DANGEROUS.
  51. NO_ASK="`echo ${PROGNAME} | grep it | wc -c`"
  52. NO_ASK=`echo ${NO_ASK}`
  53. tempfile="/usr/tmp/zap_temp_$$"
  54.  
  55. if [ $# -eq 0 ]
  56. then
  57.   echo "${PROGNAME}: USAGE: ${PROGNAME} cmd_name"
  58.   exit 1
  59. fi
  60.  
  61. trap "exitproc" 1 2 3 15
  62.  
  63. # Functions
  64. exitproc() {
  65.  if [ -n "${tempfile}" ]; then
  66.   rm -f ${tempfile}*
  67.  fi
  68.  exit
  69. }
  70.  
  71. # Determine what type of machine we have here.
  72. mach_type=`uname -a`
  73. case $mach_type in
  74.   *SunOS*|*SunOs*|*sun4c*|*sun4*)
  75.       machine_type="Sun"
  76.       PSCMD="ps -auxw"
  77.       COLS=57
  78.       AWKCMD=nawk
  79.       ;;
  80.   *IP*|*IRIX*)
  81.       machine_type="Sgi"
  82.       PSCMD="ps -ef"
  83.       COLS=48
  84.       AWKCMD=nawk
  85.       ;;
  86.   *AIX*)
  87.       machine_type="Ibm"
  88.       PSCMD="ps -ef"
  89.       COLS=48
  90.       AWKCMD=awk
  91.       ;;
  92.   *NEWS*)
  93.       machine_type="SONY"
  94.       PSCMD="ps -ef"
  95.       COLS=48
  96.       AWKCMD=nawk
  97.       ;;
  98.     *)
  99.       echo "Error setting machine type, '$mach_type' unknown."
  100.       exitproc
  101.       ;;
  102. esac
  103.  
  104.  
  105. # Get the terminal line.
  106. TERM=`/bin/tty`
  107.  
  108. # Reduce output by match strings.
  109. for string in $*; do
  110.  if [ ! -f ${tempfile}a ]; then
  111.   ${PSCMD} | grep ${string} | grep -v grep | grep -v ${PROGNAME} > ${tempfile}a
  112.  else
  113.   grep ${string} ${tempfile}a > ${tempfile}b
  114.   if [ -s ${tempfile}b ]; then
  115.    mv ${tempfile}b ${tempfile}a
  116.   else
  117.    echo "${PROGNAME}: No Match"
  118.    exitproc
  119.   fi
  120.  fi
  121. done
  122.  
  123. if [ -s ${tempfile}a ]; then
  124.  ${AWKCMD} '{
  125.   printf("Command: %s\n ",substr($0,"'${COLS}'"))
  126.   if( "'${NO_ASK}'" > 0 )
  127.   {
  128.      ANS = "y"
  129.   }
  130.   else
  131.   {
  132.      printf("Do you want to kill it? [%5d] : ",$2)
  133.      getline ANS < "'${TERM}'" 
  134.      close("'${TERM}'")
  135.      if( length(ANS) == 0 )
  136.          ANS = "n"
  137.  
  138.   }
  139.  
  140.   if( index("yYyesYES",ANS) > 0)
  141.   {
  142.     system("/bin/kill -9 " $2 )
  143.     printf("Process id %5d killed.\n",$2) 
  144.   }
  145.   else if(index("qQquitQUIT",ANS) > 0)
  146.   {
  147.      exit
  148.   }
  149.  }' ${tempfile}a
  150. else
  151.  echo "${PROGNAME}: No Match."
  152. fi
  153.  
  154. exitproc
  155.  
  156. ----------- 8< ---- end zap ---------- 8< ---------------------------
  157.  
  158. Enjoy :-)
  159. -------------------------------------------------------------------------------
  160. Hemant Shah                            | All the opinions expressed are my own
  161. Fermi National Accelerator Laboratory  | and does not necessarily reflect
  162. Systems Integration Group              | those of Fermilab.
  163. E-mail :shah@fnal.fnal.gov             |
  164.  Voice : (708) 840-8071                   | I haven't lost my mind
  165.    Fax : (708) 840-2783                | it's backed on tape somewhere.
  166. -------------------------------------------------------------------------------
  167.