监控shell脚本:monitor.sh
注意:脚本中“source /etc/profile”是因为crontab执行脚本无法取到环境变量,导致进程启动不起来,所以这个必须加上
#! /bin/sh
source /etc/profile
cd /root
procnum=`ps -ef|grep project.jar|grep -v grep|wc -l`
if [ $procnum -eq 0 ]
then
echo `date +%Y-%m-%d` `date +%H:%M:%S` "restart service" >>/root/logs/restart.log
./start.sh
fi
启动java进程脚本:start.sh
#! /bin/sh
source /etc/profile
kill -9 `ps -ef|grep 'project.jar'|grep -v grep|awk '{print $2}'`
nohup java -jar project.jar >project.log 2>&1 &
tail -f project.log
将monitor.sh脚本加入crontab
#进入crontab
crontab -e
#填写一下内容
#开机自启动任务
@reboot /bin/sh /root/monitor.sh
#每分钟执行一次start.sh脚本
* * * * * /bin/sh /root/monitor.sh