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'` "	 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'` "	 program start" >> ${LOGNAME}
    fi  

    sleep 2

done
exit 0

  

原文地址:https://www.cnblogs.com/allenhaozi/p/5984356.html