[工具]进程守护脚本

来源:互联网 发布:Buffer cache linux 编辑:程序博客网 时间:2024/05/21 09:04
#!/bin/bash
##Daemon Script##
##Please put this Script in the Program Directory##
##Create by zph##

SleepTime=10
ProcUser=appadmin
ProcMaster=java
ProgramBase=/opt
ProcProgram=startup.sh

if [ `dirname $0` = '.' ];then
        ProcName=`pwd|xargs basename`
else
        ProcName=`dirname $0|xargs basename`
fi
DaemonLog=${ProcName}_Daemon.log
if [ ! -f $DaemonLog ];then
        touch $DaemonLog
fi

while true;do
        ProcStatus=`ps -ef |grep $ProcMaster|grep "$ProgramBase/$ProcName"|grep -v grep|wc -l`
        if [ `whoami` != $ProcUser ];then
                UserStat="su - $ProcUser -c "
        else
                UserStat=""
        fi

        if [ $ProcStatus = 0 ];then
                source /etc/profile
                $UserStat $ProgramBase/$ProcName/bin/$ProcProgram >/dev/null 2>&1
                echo "$(date +%Y-%m-%d_%H:%M:%S)Process is Down!!!Now Restart!" >>$DaemonLog
        #else
                #echo "$(date +%Y-%m-%d_%H:%M:%S)Process is Running!!!">>$DaemonLog
        fi
        sleep $SleepTime
done
0 0