1. 编写脚本
在jar目录下创建脚本,例如:sys.sh
while :
do
run=$(ps -ef |grep "Kenner-service-sys-1.0-SNAPSHOT-exec.jar" |grep -v "grep")
if [ "$run" ] ; then
echo "The service is alive!"
else
echo "The service was shutdown!"
echo "Starting service ..."
nohup java -jar Kenner-service-sys-1.0-SNAPSHOT-exec.jar &
echo "The service was started!"
fi
sleep 10
done
这里每10秒种监听一次
2.启动脚本
注意:直接启动可能会报无权限,需要以下两个步骤
# 1.授权
chmod 777 sys.sh
# 2.处理脚本无法运行异常:报错syntax error near unexpected token `$'\r''解决方法
sed -i 's/\r//g' sys.sh
3.启动脚本
#启动脚本并打印日志sysLog.txt
nohup ./sys.sh > sysLog.txt &
此时脚本会在后台运行,每10秒监听一次服务状态,若宕机,立即重启