原理是使用Linux Crontab 定时任务
1、先编写执行脚本/mnt/bindmonitor.sh
#! /bin/sh
#查询进程ID(test-1.0.0.jar是我的程序名字,这里可以改成你所需要的逻辑,可以查进程名、进程id等等)
pid=$(ps -ef|grep test.jar |grep -v grep |awk '{print $2}')
if [ -n "${pid}" ]
then
#echo '服务正常,不理,pid:'${pid1}
fi
if [ -z "${pid}" ]
then
echo '服务未启动或已关闭,现在重新启动'
#这里加上你的程序启动命令
nohup java -Xms128m -Xmx128m -jar test.jar&
fi
给一下权限:
chmod +x bindmonitor.sh
先执行命令,进入crontab编辑模式
crontab -uroot -e
加入下发命令,含义是每2分钟运行一次(更多用法可以查看文末参考链接)
*/2 * * * * * * * * . /etc/profile; /bin/sh /mnt/bindmonitor.sh
. /etc/profile;是引用环境变量,不然的话sh脚本可能会出现不生效问题
参考博客: