linux定时检查jar包运行并重启

linux后台运行jar包,经常以外退出,定时计划运行sh检查jar包运行并重启


新建restart.sh

root@instance-1:~/sub#touch restart.sh

权限设置744(需要注意定时任务的用户有没有运行权限)

root@instance-1:~/sub#chmod 744 restart.sh
编辑restart.sh 文件:
#!/bin/sh
export LANG="en_US.UTF-8"
export JAVA_HOME=/usr/java/jdk1.8.0_151
export PATH=$JAVA_HOME/bin:$PATH  
export CLASSPATH=.:$JAVA_HOME/dt.jar:$JAVA_HOME/tools.jar 
#java环境变量

jarDir="/root/sub"
jar="SSR_subscribe-1.0.0.jar"
logFile="/root/sub/restart.log"

restart(){
        cd ${2}
        if [ ! -f ${1} ];then
                echo "jar not exist" >> ${logFile}
                return
        fi
        (java -jar ${1} &)
#端口映射       iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
}

check(){
        count=$(ps -ef | grep ${1} | grep -v "grep")
        if [ -n "${count}" ];then
#               echo "`date` ${1} is running" >> ${logFile}
               echo "`date` ${1} s running"
        else
                restart ${1} ${2}
                echo "`date` ${1} is down. restartig" >> ${logFile}
#               echo "`date` ${1} is down"

        fi
}

check ${jar} ${jarDir}

定时任务,每小时检查jar包运行,如果退出重启

root@instance-1:~#crontab -e
* */1 * * * /bin/sh /root/sub/restart.sh
参考: crontab用户权限


你可能感兴趣的:(日志)