#! /bin/sh
#
# See contrib/init.resin for /etc/rc.d/init.d startup script
#
# resin.sh can be called like apachectl
#
# resin.sh -- execs resin in the foreground
# resin.sh start -- starts resin in the background
# resin.sh stop -- stops resin
# resin.sh restart -- restarts resin
#
# resin.sh will return a status code if the wrapper detects an error, but
# some errors, like bind exceptions or Java errors, are not detected.
#
# To install, you'll need to configure JAVA_HOME and RESIN_HOME and
# copy contrib/init.resin to /etc/rc.d/init.d/resin. Then
# use "unix# /sbin/chkconfig resin on"
PCK_TAG=pcheck
export=PCK_TAG
#JAVA_HOME
JAVA_HOME=/usr/local/jdk1.6.0_29
export JAVA_HOME
#本脚本的绝对路径
EXEC_START="/usr/local/sns/snsconnector.sh"
#日志输入路径
WATCH_LOG="/usr/local/sns/watchdog.log"
export WATCH_LOG
#执行脚本
RESIN="bin/httpd.sh"
#执行多个resin的多路径
EXEC="/usr/local/sns/snsapi/resin3 /usr/local/sns/snsapi2/resin3"
array=($EXEC)
length=${#array[@]}
echo $length
for ((i=0; i<$length; i++))
do
echo ${array[$i]}
done
#####################################################
# Find a PID for the pid file
#####################################################
for ((i=0; i<$length; i++))
do
RESIN_HOME=${array[$i]}
SNS_PIDS="$SNS_PIDS $RESIN_HOME/snsconnector.pid "
done
pids=($SNS_PIDS)
size=${#pids[@]}
echo $size
for ((i=0; i<$size; i++))
do
echo ......${pids[$i]}
done
#
SNS_TAG=snstag
if test -n "${JAVA_HOME}"; then
if test -z "${JAVA_EXE}"; then
JAVA_EXE=$JAVA_HOME/bin/java
fi
fi
#
# trace script and simlinks to find the wrapper
#
if test -z "${RESIN_HOME}"; then
script=`/bin/ls -l $0 | awk '{ print $NF; }'`
while test -h "$script"
do
script=`/bin/ls -l $script | awk '{ print $NF; }'`
done
bin=`dirname $script`
RESIN_HOME="$bin/.."
fi
running()
{
[ -f $1 ] || return 1
PID=$(cat $1)
echo ==========$PID==========
ps -p $PID >/dev/null 2>/dev/null || return 1
return 0
}
zombiling()
{
PID=$(cat $1)
echo "zombiling $PID" >>$WATCH_LOG
IMZOMBIE=`ps -el |grep $PID|awk '{print $2}'` >/dev/null 2>/dev/null
imarray=($IMZOMBIE)
echo "SNSConnector process current run status is: ${imarray[0]}" >>$WATCH_LOG
echo "SNSConnector process current run status is: $IMZOMBIE"
echo "`date '+%Y-%m-%d %T'` [INFO] SNSConnector process current run status is: $IMZOMBIE" >>$WATCH_LOG
if [ ! -z `echo ${imarray[0]}|grep '^[TZ]'` ]
then
echo "SNSConnector process is zombied. now restart the service."
echo "`date '+%Y-%m-%d %T'` [WARN] SNSConnector process is zombied. now restart the service." >>$WATCH_LOG
return 0
fi
return 1
}
usage()
{
echo "Usage: $0 {start|stop|status} [ CONFIGS ... ] "
exit 1
}
[ $# -gt 0 ] || usage
ACTION=$1
MAINTE_CHECK=$2
ZOMBIE_CHECK=$3
#####################################################
# Get current script belongs to User and Group.
#####################################################
[ -z ${SCRIPT_NAME} ] && SCRIPT_NAME=httpd.sh
if [ -f ${RESIN_HOME}/bin/${SCRIPT_NAME} ]
then
CURRENT_GROUP="$(echo `ls -l ${RESIN_HOME}/bin/${SCRIPT_NAME}` | awk '{print $4}')";
CURRENT_USER="$(echo `ls -l ${RESIN_HOME}/bin/${SCRIPT_NAME}` | awk '{print $3}')";
else
echo "`date '+%Y-%m-%d %T'` ERROR: Current start service script is:${RESIN_HOME}/bin/${SCRIPT_NAME}" >>$WATCH_LOG
echo "`date '+%Y-%m-%d %T'` ERROR: The SNS start file is not exist."
fi
setCrontab()
{
echo "setCrontab...";
crontab_tmp=$(mktemp -p "${RESIN_HOME}/bin/")
if [ x$CURRENT_USER != x$(whoami) ]
then
echo "* * * * * su - $CURRENT_USER -c "${EXEC_START} start $SNS_TAG" > /dev/null 2>&1" >> "${crontab_tmp}"
echo "* * * * * sleep 10; su - $CURRENT_USER -c "${EXEC_START} start $SNS_TAG" > /dev/null 2>&1" >> "${crontab_tmp}"
echo "* * * * * sleep 20; su - $CURRENT_USER -c "${EXEC_START} start $SNS_TAG" > /dev/null 2>&1" >> "${crontab_tmp}"
echo "* * * * * sleep 30; su - $CURRENT_USER -c "${EXEC_START} start $SNS_TAG" > /dev/null 2>&1" >> "${crontab_tmp}"
echo "* * * * * sleep 40; su - $CURRENT_USER -c "${EXEC_START} start $SNS_TAG" > /dev/null 2>&1" >> "${crontab_tmp}"
echo "* * * * * sleep 50; su - $CURRENT_USER -c "${EXEC_START} start $SNS_TAG $PCK_TAG" > /dev/null 2>&1" >> "${crontab_tmp}"
elif [ x$CURRENT_USER == x$(whoami) ]
then
echo "* * * * * "${EXEC_START} start $SNS_TAG" > /dev/null 2>&1" >> "${crontab_tmp}"
echo "* * * * * sleep 10; "${EXEC_START} start $SNS_TAG" > /dev/null 2>&1" >> "${crontab_tmp}"
echo "* * * * * sleep 20; "${EXEC_START} start $SNS_TAG" > /dev/null 2>&1" >> "${crontab_tmp}"
echo "* * * * * sleep 30; "${EXEC_START} start $SNS_TAG" > /dev/null 2>&1" >> "${crontab_tmp}"
echo "* * * * * sleep 40; "${EXEC_START} start $SNS_TAG" > /dev/null 2>&1" >> "${crontab_tmp}"
echo "* * * * * sleep 50; "${EXEC_START} start $SNS_TAG $PCK_TAG" > /dev/null 2>&1" >> "${crontab_tmp}"
fi
tail "${crontab_tmp}"
crontab "${crontab_tmp}"
rm -f ${crontab_tmp}
return 0
}
case "$ACTION" in
start)
echo "`date '+%Y-%m-%d %T'` [INFO] snsconnector service start begin.$MAINTE_CHECK $ZOMBIE_CHECK " >>$WATCH_LOG
for ((i=0; i<$length; i++))
do
RESIN_HOME=${array[$i]}
RUN_CMD=$RESIN_HOME/$RESIN
SNS_PID=${pids[$i]}
echo -n "$RESIN_HOME Starting snsconnector: "
if [ x$MAINTE_CHECK == x$SNS_TAG ] && [ -f "$RESIN_HOME/$SNS_TAG" ]
then
echo "Maintenance period,can't start sns service."
exit 1
fi
echo "`date '+%Y-%m-%d %T'` [INFO] snsconnector service start begin.[x$ZOMBIE_CHECK] [x$PCK_TAG] " >>$WATCH_LOG
if [ x$ZOMBIE_CHECK == x$PCK_TAG ] && zombiling $SNS_PID
then
PID=`cat $SNS_PID 2>/dev/null`
echo "`date '+%Y-%m-%d %T'` snsconnector(pid $PID $SNS_PID) is zombie." >>$WATCH_LOG
kill -9 $PID
echo "`date '+%Y-%m-%d %T'` snsconnector(pid $SNS_PID) is stopped." >>$WATCH_LOG
continue
else
if [ x$ZOMBIE_CHECK == x$PCK_TAG ]
then
echo "snsconnector(pid `cat $SNS_PID`) is not zombie." >>$WATCH_LOG
fi
fi
echo "`date '+%Y-%m-%d %T'` [INFO] >>>>>>>>>>[x$ZOMBIE_CHECK] [x$PCK_TAG] " >>$WATCH_LOG
if [ -f $SNS_PID ]
then
if running $SNS_PID
then
echo "Already Running."
echo "`date '+%Y-%m-%d %T'` snsconnector running pid is:"`cat $RESIN_HOME/snsconnector.pid` >>$WATCH_LOG
#exit 1
continue
else
# dead pid file - remove
rm -f $SNS_PID
fi
fi
if [ x$CURRENT_USER != x ] && [ x$CURRENT_USER != x$(whoami) ]
then
touch $SNS_PID
chown $CURRENT_USER:$CURRENT_GROUP $SNS_PID
su - $CURRENT_USER -c "
$RUN_CMD > /dev/null 2>&1 &
PID=\$!
disown \$PID
echo \$PID > $SNS_PID"
echo pid=$PID
echo sns_pid=$SNS_PID
elif [ x$CURRENT_USER == x$(whoami) ]
then
echo ===$RUN_CMD===
touch $SNS_PID
eval "
$RUN_CMD > /dev/null 2>&1 &
PID=\$!
disown \$PID
echo \$PID > $SNS_PID"
echo pid=$PID
echo sns_pid=$SNS_PID
fi
sleep 5
if running $SNS_PID
then
rm -f $RESIN_HOME/$SNS_TAG
echo "OK"
echo "pid is `cat $SNS_PID 2>/dev/null`"
echo "STARTED AT `date`"
echo "`date '+%Y-%m-%d %T'` [INFO] snsconnector service start OK." >>$WATCH_LOG
else
echo "FAILED."
echo "`date '+%Y-%m-%d %T'` [ERROR] snsonnector service start FAILED." >>$WATCH_LOG
fi
done
setCrontab;
exit 1
;;
stop)
echo "`date '+%Y-%m-%d %T'` [INFO] snsconnector service stop begin." >>$WATCH_LOG
for ((i=0; i<$length; i++))
do
RESIN_HOME=${array[$i]}
SNS_PID=${pids[$i]}
echo -n "$RESIN_HOME Stopping snsconnector... "
if running $SNS_PID
then
PID=`cat $RESIN_HOME/$SNS_PID 2>/dev/null`
TIMEOUT=60
while running $SNS_PID && [ $TIMEOUT -gt 0 ]
do
kill $PID 2>/dev/null
echo -n "."
sleep 1
let TIMEOUT=$TIMEOUT-1
done
[ $TIMEOUT -gt 0 ] || kill -9 $PID 2>/dev/null
rm -f $SNS_PID
echo OK
echo "`date '+%Y-%m-%d %T'` [INFO] $RESIN_HOME snsconnector service stop OK." >>$WATCH_LOG
else
echo "$SNS_PID snsconnector service already stopped,stop failed."
echo "`date '+%Y-%m-%d %T'` [ERROR] $RESIN_HOME snsconnector service already stopped,stop FAILED." >>$WATCH_LOG
fi
touch $RESIN_HOME/$SNS_TAG
done
exit 1
;;
status)
echo "`date '+%Y-%m-%d %T'` [INFO] snsconnector service status." >>$WATCH_LOG
for ((i=0; i<$length; i++))
do
RESIN_HOME=${array[$i]}
SNS_PID=${pids[$i]}
if running $SNS_PID
then
PID=`cat $SNS_PID 2>/dev/null`
echo "snsconnector(pid $PID) is running."
echo "`date '+%Y-%m-%d %T'` [INFO] $RESIN_HOME snsconnector(pid $PID) is running." >>$WATCH_LOG
else
rm -f $SNS_PID
echo "snsconnector has stoped."
echo "`date '+%Y-%m-%d %T'` [INFO] $RESIN_HOME snsconnector(pid $PID) has stoped." >>$WATCH_LOG
fi
done
exit 1
;;
*)
usage
;;
esac
exit 0
resin的自动拉起脚本,检测僵死进程
最新推荐文章于 2020-09-06 19:07:49 发布