[ Restart script for IPL ]
#!/bin/sh
#
#set -x
P_BIT=$1 # start/stop/chk-on/chk-off/status
if [ $# -ne 1 ]
then
echo "Usage: $0 {start|stop|chk-on|chk-off|status}"
exit 2
fi
# --------------------------------------------------------------- #
# Basic Env
# --------------------------------------------------------------- #
# start/stop command
BOOT_CMD="start.sh"
KILL_CMD="stop.sh"
# status check command
# To use "@" as delemiter for multiple check
CHK_CMD="ps -ef | grep PATTERN"
# --------------------------------------------------------------- #
# 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 }'`
# wc: 0 = stopped, 1 or higher = started
if [ ${wc} -gt 0 ]
then
wc=1
fi
now_cnt=`expr ${now_cnt} + ${wc}`
i=`expr ${i} + 1`
done
# Return: 100 = up, 110 = down, 111 = partial up/down
if [ ${now_cnt} -eq ${chk_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.\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})\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}"
exit 2
;;
esac
######################## End of script ########################
No comments:
Post a Comment