在linux系統中創建開機自啟動應用和應用自啟腳本。腳本如下:
start.sh --啟動目標應用的腳本
keeper.sh --循環自啟目標應用腳本,用於應用進程掛掉后的自啟
install.sh --應用安裝腳本,第一次部署應用時執行
start.sh
#!/bin/sh #指定后台日志目錄及日志文件名 logfilePath="/home/myapp/logs/" logfileName=$logfilePath"nohup.log" #如果不存在則創建 if [ ! -d "$logfilePath" ]; then mkdir -p "$logfilePath" touch "$logfileName" fi
#啟動目標應用腳本【需要修改】 nohup java -jar /home/myapp/myapp-0.0.1-SNAPSHOT.jar --spring.config.location=/home/myapp/config/application.properties >/home/myapp/logs/nohup.log 2>&1 &
keeper.sh
#!/bin/sh #應用bin目錄【需要修改】 appStartFile="/home/didi/bin/start.sh" #應用進程名稱【需要修改】 progressName="didijoin-0.0.1-SNAPSHOT.jar" while true;do #目標應用進程名稱【需要修改】 PIDS=`ps -ef|grep $progressName | grep -v grep|awk '{print $2}'` if [ "$PIDS" = "" ];then echo "the program[$progressName] is not running,restart it!" echo "restart..." sh "$appStartFile" fi sleep 3 done
install.sh
#!/bin/sh #keeper.sh文件完整目錄【需要修改】 keeperFilePath="/home/myapp/bin/keeper.sh" #程序名稱【需要修改】 echo "install program[myapp]..." #向rc/local文件末尾追加啟動keeper任務的腳本 echo "sh $keeperFilePath">>/etc/rc.local echo "install program success!" echo "start program..." #日志目錄【需要修改】 nohup sh "$keeperFilePath" >/home/myapp/logs/install.log 2>&1 & echo "start program success!"