Simple Daemon Shell

PROPATH="/var/www/html/"
PROGRAM="vertical"
LOGNAME="/tmp/monitor.vertical.go.log"

while true ; do

    NUM=`ps aux | grep ${PROGRAM} | grep -v grep | wc -l`
    echo ${NUM}
    if [ "${NUM}" -lt "1" ]; then
        #log name must distinguish program name
        sudo nohup ${PROPATH}${PROGRAM} >> ${LOGNAME} 2>&1 &
        echo -e `date +'%Y-%m-%d %H:%M:%S'` "\t program start" >> ${LOGNAME}
    fi  

    #kill Zombie program
    NUM_STAT=`ps aux | grep ${PROGRAM} | grep T | grep -v grep | wc -l`
    if [ "${NUM_STAT}" -gt "0" ]; then
        killall -9 ${PROGRAM}
        sudo nohup ${PROPATH}${PROGRAM} >> ${LOGNAME} 2>&1 &
        echo -e `date +'%Y-%m-%d %H:%M:%S'` "\t program start" >> ${LOGNAME}
    fi  

    sleep 2

done
exit 0

  

posted @ 2016-10-21 13:47  hao.ma  阅读(168)  评论(0编辑  收藏  举报