编写monitor.sh文件来监听进程是否成功执行
#!/bin/sh
source /etc/profile
source ~/.bash_profile
restart_name="/data/logs/restart.log" #重启脚本的日志,保证可写入,保险一点执行 chmod 777 restart.log
success_name="/data/logs/success.log" #成功脚本
#查看进程是否存在
# shellcheck disable=SC2006
result=`ps -ef | grep -w 项目名 | grep -v grep | wc -l`
if [ $result -le 0 ]; then
#不存在, 重启
pid=`nohup /data/项目名 -configPath="/data/" >/data/nohup.out 2>&1 &`
echo $pid `date` "项目 is restart" >> $restart_name #把重启的进程号、时间 写入日志
else
myPid=$(ps -ef | grep -w 项目名 | grep -v grep|awk '{print $2}')
#echo $myPid
echo `date` ",项目名 is success,current status is:" `ps -ef|grep $myPid|grep -v grep|tr -s " "` >> $success_name #把成功的时间 写入日志
fi
使用crontab来定时执行
①crontab -e
* * * * * sh monitor.sh
②wq 保存上述命令即可