home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.unix.shell:3712 comp.unix.questions:10568
- 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
- From: shah@fnsony.fnal.gov (Hemant Shah)
- Newsgroups: comp.unix.shell,comp.unix.questions
- Subject: Re: Need Script To Kill Process Using Substring Of Application Name
- Message-ID: <2238@fnnews.fnal.gov>
- Date: 31 Aug 92 17:21:44 GMT
- References: <x6gnwvc.westes@netcom.com> <rqhnklc.tunxis@netcom.com>
- Sender: news@fnnews.fnal.gov
- Reply-To: shah@fnalb.fnal.gov
- Followup-To: comp.unix.shell
- Organization: Fermi National Accelerator Laboratory, Batavia, IL
- Lines: 152
- Nntp-Posting-Host: fnsony.fnal.gov
-
- Here's the script that I use.
-
- ---------- 8< ---- begin zap ---------- 8< ---------------------------
- #!/bin/sh
- #
- # Name: zap (zapit)
- # Programmer:
- # Hemant Shah (708) 840-8071
- # Frank Koenen (708) 840-8042
- # Fermilab
- # Systems Integration Group
- # March 31, 1992
- #
- # Description:
- # Kills processes by process name. e.g. zap xclock
- # It will ask for conformation before killing the process. If you do not
- # want it to ask for conformation then create a soft link to zapit.
- # e.g. ln -s zap zapit
- # If you type "zapit xclock", then it will not ask you for yes/no.
- # NOTE: This can be dangerous if used without caution.
- #
- # Also if you give more then argument on command line it will only kill the
- # process that has all the arguments in the output from ps command.
- #
- # Example: There are more then one users running xterm, but you only want to
- # kill xterm running under username shah, then type following:
- # zap shah xterm
- #
- #
- # get the name of the program
- PROGNAME=`basename ${0}`
-
- # If the program contains "it" in it e.g. zapit then do not ask for
- # conformation, just kill the process.
- # NOTE: THIS CAN BE DANGEROUS.
- NO_ASK="`echo ${PROGNAME} | grep it | wc -c`"
- NO_ASK=`echo ${NO_ASK}`
- tempfile="/usr/tmp/zap_temp_$$"
-
- if [ $# -eq 0 ]
- then
- echo "${PROGNAME}: USAGE: ${PROGNAME} cmd_name"
- exit 1
- fi
-
- trap "exitproc" 1 2 3 15
-
- # Functions
- exitproc() {
- if [ -n "${tempfile}" ]; then
- rm -f ${tempfile}*
- fi
- exit
- }
-
- # Determine what type of machine we have here.
- mach_type=`uname -a`
- case $mach_type in
- *SunOS*|*SunOs*|*sun4c*|*sun4*)
- machine_type="Sun"
- PSCMD="ps -auxw"
- COLS=57
- AWKCMD=nawk
- ;;
- *IP*|*IRIX*)
- machine_type="Sgi"
- PSCMD="ps -ef"
- COLS=48
- AWKCMD=nawk
- ;;
- *AIX*)
- machine_type="Ibm"
- PSCMD="ps -ef"
- COLS=48
- AWKCMD=awk
- ;;
- *NEWS*)
- machine_type="SONY"
- PSCMD="ps -ef"
- COLS=48
- AWKCMD=nawk
- ;;
- *)
- echo "Error setting machine type, '$mach_type' unknown."
- exitproc
- ;;
- esac
-
-
- # Get the terminal line.
- TERM=`/bin/tty`
-
- # Reduce output by match strings.
- for string in $*; do
- if [ ! -f ${tempfile}a ]; then
- ${PSCMD} | grep ${string} | grep -v grep | grep -v ${PROGNAME} > ${tempfile}a
- else
- grep ${string} ${tempfile}a > ${tempfile}b
- if [ -s ${tempfile}b ]; then
- mv ${tempfile}b ${tempfile}a
- else
- echo "${PROGNAME}: No Match"
- exitproc
- fi
- fi
- done
-
- if [ -s ${tempfile}a ]; then
- ${AWKCMD} '{
- printf("Command: %s\n ",substr($0,"'${COLS}'"))
- if( "'${NO_ASK}'" > 0 )
- {
- ANS = "y"
- }
- else
- {
- printf("Do you want to kill it? [%5d] : ",$2)
- getline ANS < "'${TERM}'"
- close("'${TERM}'")
- if( length(ANS) == 0 )
- ANS = "n"
-
- }
-
- if( index("yYyesYES",ANS) > 0)
- {
- system("/bin/kill -9 " $2 )
- printf("Process id %5d killed.\n",$2)
- }
- else if(index("qQquitQUIT",ANS) > 0)
- {
- exit
- }
- }' ${tempfile}a
- else
- echo "${PROGNAME}: No Match."
- fi
-
- exitproc
-
- ----------- 8< ---- end zap ---------- 8< ---------------------------
-
- Enjoy :-)
- -
- -------------------------------------------------------------------------------
- Hemant Shah | All the opinions expressed are my own
- Fermi National Accelerator Laboratory | and does not necessarily reflect
- Systems Integration Group | those of Fermilab.
- E-mail :shah@fnal.fnal.gov |
- Voice : (708) 840-8071 | I haven't lost my mind
- Fax : (708) 840-2783 | it's backed on tape somewhere.
- -------------------------------------------------------------------------------
-