Springboot專案設定服務,開機自啟動(centos)
阿新 • • 發佈:2020-12-23
技術標籤:springbootLinuxlinuxcentosjava
感覺都很久沒寫東西了.今天記錄以下基於
Centos7
,把Java
專案設定成服務,並開機自啟動
- 編寫專案啟動指令碼
#!/bin/bash
APP_NAME=springboot-test
APP_PATH=/data/springboot/test/
export JAVA_HOME=/data/java/jdk1.8
export JRE_HOME=${JAVA_HOME}/jre
usage(){
echo "請輸入以下內容 [start|stop]"
exit 1
}
stop(){
echo "準備關閉當前專案已存在程序"
tpid=`ps -ef|grep ${APP_NAME}.jar|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
echo "關閉程序: ${tpid}"
kill -9 $tpid
echo "關閉完成"
else
echo '專案未執行'
fi
}
start(){
stop
echo "開始啟動"
rm -f ${APP_PATH}${APP_NAME} .pid
nohup ${JRE_HOME}/bin/java -jar ${APP_PATH}${APP_NAME}.jar &
echo $! > ${APP_PATH}${APP_NAME}.pid
}
case $1 in
"start")
start
;;
"stop")
stop
;;
*)
usage
;;
esac
exit 0
- 指令碼授權
chmod +x /data/springboot/test/starter.sh
- 設定自定義服務
vim /lib/systemd/system/springboot-test.service
- 編寫內容
[Unit]
Description=springboot-test-server
After=network.target
[Service]
Type=forking
ExecStart=/data/springboot/test/starter.sh start
ExecRestart=/data/springboot/test/starter.sh start
ExecStop=/data/springboot/test/starter.sh stop
[Install]
WantedBy=multi-user.target
- 重新載入配置
systemctl daemon-reload
- 設定服務自啟動
systemctl enable springboot-test.service
好了到這我們Java專案就被設定成服務了,並開機自啟動了
- 常用服務命令
# 開機啟動
systemctl enable springboot-test.service
# 啟動
systemctl start springboot-test.service
# 關閉
systemctl stop springboot-test.service
# 重啟
systemctl restart springboot-test.service
# 檢視狀態
systemctl status springboot-test.service
# 重新載入配置
systemctl daemon-reload
- 檢視所有的開機啟動項
systemctl list-unit-files|grep enabled