原理是使用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腳本可能會出現不生效問題
參考博客: