#!/bin/ksh

#

APP=`basename $0`

 

GREP_OUT="grep|$APP|xterm|PID"

WAIT_TIME=3

FAILED=1

SUCCESS=0

 

GREP_TARG=""

GREP_TARG2="."

KILL_SIG="-9"

KILL_CMD="kill"

INTERACTIVE="ON"

KILL_STATUS=$FAILED

 

function PrintUsage

{

  echo

  echo "    USAGE:  $APP target_string [-grep target_string_2] [-signal] [-i]"

  echo

  echo "  PURPOSE:  $APP will shut down all currently running processes"

  echo "            that match the target string, by sending a kill signal."

  echo

  echo "  DESCRIPTION:"

  echo "            target_string_2 is used to further define the victim "

  echo "            process.  $APP applies target_string to the output of "

  echo "            a ps -fu \$LOGNAME command then applies a separate grep"

  echo "            for target_string_2, if supplied."

  echo

  echo "            Kill signal defaults to Unix standard if not specified."

  echo "            The -signal parameter can be used to override thedefault"

  echo "            with the \"signal\" provided."

  echo

  echo "            The -i parameter is used to turn off the interactive"

  echo "            aspect of $APP.  This will supress all requests for "

  echo "            conformation before kills are made.  Use at your own risk."

  echo

  echo " EXAMPLES:  $APP bobo        -- Kill bobo."

  echo "            $APP bobo -9     -- Kill bobo with a extreme prejudiceand"

  echo "                                 don't tell me about it."

  echo

  exit 1

}

if [[ $1 = "-h" ]]; then

  PrintUsage | more

  exit 0

fi

 

if [[ $1 = "" ]]; then

  echo "\n!!!! NO KILL TARGET !!!\n"

  PrintUsage | more

  exit $KILL_STATUS

else

  GREP_TARG="$1"; shift

fi

 

while [[ $1 != "" ]]; do

  case $1 in

    -grep) GREP_TARG2=$2

           shift

           shift

           ;;

    -h)  PringUsage | more

         exit 0

         ;;

    -i)  INTERACTIVE="OFF"

         shift

         ;;

     *)  KILL_SIG="$1"

         shift

         ;;

  esac

done

 

if [[ $GREP_TARG2 = "." ]]; then

  NONE_MSG="$GREP_TARG is not running under $LOGNAME."

  FAIL_MSG="Failed to kill $GREP_TARG in allotted time!\n"

  OK_MSG="\"$GREP_TARG\" successfully killed under $LOGNAME."

  RESTART_MSG="\"$GREP_TARG\" successfully killed and RESTARTED under$LOGNAME.\n"

else

  NONE_MSG="$GREP_TARG with $GREP_TARG2 is not running under $LOGNAME."

  FAIL_MSG="Failed to kill $GREP_TARG with $GREP_TARG2 in allotted time!\n"

  OK_MSG="$GREP_TARG with $GREP_TARG2 successfully killed under $LOGNAME."

  RESTART_MSG="$GREP_TARG with $GREP_TARG2 successfully killed and RESTARTED under $LOGNAME.\n"

fi

 

SISCFG_PID=`ps -fu $LOGNAME | egrep "$GREP_TARG" | grep "$GREP_TARG2" |egrep -v "$GREP_OUT" | awk '{print $2}' | grep -v PID`

 

if [ -n "$SISCFG_PID" ]; then

 

        if [[ $INTERACTIVE = "ON" ]]; then

          echo

          ps -fu $LOGNAME | egrep "$GREP_TARG" | grep "$GREP_TARG2" |egrep -v "$GREP_OUT"

          echo

          read OK?"Kill them all using $KILL_SIG ? (Y or Enter): "

        else

          OK="Y"

        fi

 

        if [[ $OK = "Y" || $OK = "y" ]]; then

          echo $KILL_CMD $KILL_SIG $SISCFG_PID

          $KILL_CMD $KILL_SIG $SISCFG_PID 2>/dev/null

          KILLSTAT=$?

          if [[ $KILLSTAT -ne 0 ]]; then

                  print "Bad mkill option or kill failed!"

                  exit 1

          fi

 

          trap "echo \"\n$APP Wait Cancelled\n\"; break" 2 15 TERM

 

          PSTAT1=`ps -fu $LOGNAME | egrep "$GREP_TARG" | grep "$GREP_TARG2" | egrep -v "$GREP_OUT" | awk '{print $2}' | grep -v PID`

#          while [[ -n `ps -fu $LOGNAME | egrep "$GREP_TARG" | grep "$GREP_TARG2" | egrep -v "$GREP_OUT" | awk '{print $2}' | grep -v PID` && $INTERACTIVE = "ON" ]]; do

#            ps -fu $LOGNAME | egrep "$GREP_TARG" | grep "$GREP_TARG2" | egrep -v "$GREP_OUT"

            echo "Waiting $WAIT_TIME seconds ..."; sleep $WAIT_TIME

#          done

 

          PSTAT=`ps -fu $LOGNAME | egrep "$GREP_TARG" | grep "$GREP_TARG2" |egrep -v "$GREP_OUT" | awk '{print $2}' | grep -v PID`

          if [[ -z $PSTAT ]]; then

            KILL_STATUS=$SUCCESS

            echo $OK_MSG

          elif [[ $PSTAT != $PSTAT2 ]]; then

            KILL_STATUS=$SUCCESS

            echo $RESTART_MSG

            ps -fu $LOGNAME | egrep "$GREP_TARG" | grep "$GREP_TARG2" |egrep -v "$GREP_OUT"

          else

            KILL_STATUS=$FAILED

            echo $FAIL_MSG

            ps -fu $LOGNAME | egrep "$GREP_TARG" | grep "$GREP_TARG2" |egrep -v "$GREP_OUT"

          fi

        fi

else

        echo $NONE_MSG

fi

 

exit $KILL_STATUS