Tuesday, January 8, 2019

▷ start/stop/status script with exact count for IPL


[ Restart script for IPL ]


#!/bin/sh
#
#set -x

P_BIT=$1 # start/stop/chk-on/chk-off/status
P_CNT=$2 # number of running count

if [ $# -ne 2 ]
then
   echo "Usage: $0 {start|stop|chk-on|chk-off|status} CNT"
   exit 2
fi

# --------------------------------------------------------------- #
# Basic Env
# --------------------------------------------------------------- #
# start/stop command
BOOT_CMD="ls -lrt"
KILL_CMD="df"

# status check command
# To use "@" as delemiter for multiple check
CHK_CMD="ps -ef | grep tomcat"
# --------------------------------------------------------------- #

# Return Code
EXIT_SUCC=0
EXIT_FAIL=1

#
unalias -a
PATH=$PATH:.
export PATH
HOST_NAME=`hostname`
WHOAMI=`whoami`

status()
{
   # Delemiter for multi-check command
   split="@"

   # Number of command to check
   chk_cnt=`echo "${CHK_CMD}" | awk -F${split} '{ print NF }'`
 
   # Number of calculated count
   now_cnt=0
 
   i=1
   while [ ${chk_cnt} -ge ${i} ]
   do
      cmd=`echo "${CHK_CMD}" | cut -d${split} -f ${i} | awk '{ $1=$1; print }'`

      if [ "`echo ${cmd} | awk '{ print $1 }'`" = "ps" ]
      then
         cmd="${cmd} | grep -v $0 | grep -v grep"
      fi
   
      printf "@ ==> (INFO)  chk_cmd[${i}] (${cmd}) checking...\n"
      printf "${cmd}" | sh
      wc=`printf "${cmd}" | sh 2> /dev/null | wc -l | awk '{ print $1 }'`
   
      now_cnt=`expr ${now_cnt} + ${wc}`
      i=`expr ${i} + 1`
   done
 
   # Return: 100 = up, 110 = down, 111 = partial up/down
   if [ ${now_cnt} -eq ${P_CNT} ]
   then
      printf "@ ==> (INFO)  It is running.\n"
      rv=100
   elif [ ${now_cnt} -eq 0 ]
   then
      printf "@ ==> (INFO)  It is NOT running.\n"
      rv=110
   else
      printf "@ ==> (WARN)  Part of it is up/down. (CHK_CNT = ${P_CNT}, NOW_CNT = ${now_cnt})\n"
      rv=111
   fi
 
   return "${rv}"
}

start()
{
   status
   rv="$?"
 
   if [ ${rv} -eq 110 ]
   then # down
      printf "@ ==> (INFO)  run_cmd(${BOOT_CMD}) starting...\n"
      printf "${BOOT_CMD}" | sh
      rc="$?"
 
      if [ ${rc} -eq 126 ] || [ ${rc} -eq 127 ] || [ ${rc} -eq 1 ]
      then
         printf "@ ==> (ERROR) run_cmd(${BOOT_CMD}) run with errors. (exit_code: ${rc})\n"
         retval=${EXIT_FAIL}
      else
         printf "@ ==> (INFO)  run_cmd(${BOOT_CMD}) successfully run. (exit_code: ${rc})\n"
         retval=${EXIT_SUCC}
      fi
   elif [ ${rv} -eq 100 ]
   then # up
      printf "@ ==> (WARN)  run_cmd(${BOOT_CMD}) already started.\n"
      retval=${EXIT_SUCC}
   else # 111 - partial up/down
      printf "@ ==> (WARN)  run_cmd(${BOOT_CMD}) skipped to run.\n"
      retval=${EXIT_FAIL}
   fi
 
   return "${retval}"
}

stop()
{
   status
   rv="$?"
 
   if [ ${rv} -eq 110 ]
   then # down
      printf "@ ==> (WARN)  run_cmd(${KILL_CMD}) already stopped.\n"
      retval=${EXIT_SUCC}
   elif [ ${rv} -eq 100 ]
   then # up
      printf "@ ==> (INFO)  run_cmd(${KILL_CMD}) stopping...\n"
      printf "${KILL_CMD}" | sh
      rc="$?"
 
      if [ ${rc} -eq 126 ] || [ ${rc} -eq 127 ] || [ ${rc} -eq 1 ]
      then
         printf "@ ==> (ERROR) run_cmd(${KILL_CMD}) run with errors. (exit_code: ${rc})\n"
         retval=${EXIT_FAIL}
      else
         printf "@ ==> (INFO)  run_cmd(${KILL_CMD}) successfully run. (exit_code: ${rc})\n"
         retval=${EXIT_SUCC}
      fi
   else # 111 - partial up/down
      printf "@ ==> (WARN)  run_cmd(${KILL_CMD}) skipped to run.\n"
      retval=${EXIT_FAIL}
   fi
 
   return "${retval}"
}

chk_loop()
{
   chk_bit=$1 # 100=up, 110=down
   chk_max=$2 # maximum second
 
   interval=3 # sleep
   max_cnt=10 # default loop count
   test ! -z "${chk_max}" && { max_cnt=`expr ${chk_max} / ${interval}`; }
 
   count=1
   while [ ${count} -le ${max_cnt} ]
   do
      printf "\n@ ==> Looping... (${count} / ${max_cnt})\n"

      status
      rv="$?"

      if [ ${rv} -eq ${chk_bit} ] ; then break; fi

      count=`expr ${count} + 1`
      sleep ${interval}
   done
 
   return "${rv}"
}

chk_on()
{
   #status
   #rv="$?"
   chk_loop "100"
   rv="$?"
 
   if [ ${rv} -eq 100 ]
   then
      printf "@ ==> (INFO)  run_cmd(${BOOT_CMD}) started.\n"
      retval=${EXIT_SUCC}
   else
      printf "@ ==> (ERROR) run_cmd(${BOOT_CMD}) stopped.\n"
      retval=${EXIT_FAIL}
   fi
 
   return "${retval}"
}

chk_off()
{
   #status
   #rv="$?"
   chk_loop "110"
   rv="$?"
 
   if [ ${rv} -eq 110 ]
   then
      printf "@ ==> (INFO)  run_cmd(${KILL_CMD}) stopped.\n"
      retval=${EXIT_SUCC}
   else
      printf "@ ==> (ERROR) run_cmd(${KILL_CMD}) started.\n"
      retval=${EXIT_FAIL}
   fi
 
   return "${retval}"
}

# --------------------------------------------------------------- #
# Main
# --------------------------------------------------------------- #

printf "@ -------------------------------------------------------------#\n"
printf "@ ==> (TASK)  Action(${P_BIT}), HostName(${HOST_NAME}), Run_User(${WHOAMI}), CHK_CNT(${P_CNT})\n"
printf "@ -------------------------------------------------------------#\n"

case "${P_BIT}" in
start)
   start
   ;;
stop)
   stop
   ;;
chk-on)
   chk_on
   ;;
chk-off)
   chk_off
   ;;
status)
   status
   ;;
*)
   echo "Usage: $0 {start|stop|chk-on|chk-off|status} CNT"
   exit 2
   ;;
esac

########################  End of script  ########################

No comments:

Post a Comment

◈ Recent Post

▷ UITest demo with TestOne (Mobile, Keypad and Drag until found tip)

[ UITest Demo Environment ] 1. UITest Solution: TestOne 2. Description 데모 설명    How to use keypad, and to drag until found.     키패드를...

◈ Popular Posts