把spring-boot項目配置爲Linux systemd服務,並註冊自啓動

把spring-boot項目配置爲Linux systemd服務之後,可以方便地使用下面幾個命令:

systemctl start myservcie.service
systemctl stop myservcie.service
systemctl restart myservcie.service
systemctl status myservcie.service

或這些:

service myservice status

service myservice start

service myservice stop

service myservice restart


1、開始配置:

vi /usr/lib/systemd/system/user-service.service
[Unit]
Description=My Service
After=syslog.target

[Service]
ExecStart=/usr/java/default/bin/java -jar /opt/app/spring-boot/myservice.jar --spring.profiles.active=dev
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target
其中,ExecStart可以加上其他參數,比如:
/usr/java/default/bin/java -jar -Xms256m -Xmx4G  -Dlog.level.console=warn -Dlog.level.ysb=info /opt/app/spring-boot/myservice.jar --spring.profiles.active=prod
另外,143是spring-boot服務被stop的時候的status code:
# service myservice stop
# service myservice status
myservice.service - My Service
   Loaded: loaded (/usr/lib/systemd/system/myservice.service; enabled)
   Active: inactive (dead) since Tue 2016-11-29 14:21:19 CST; 1s ago
  Process: 19146 ExecStart=/usr/java/default/bin/java -jar /opt/app/spring-boot/myservice.jar --spring.profiles.active=dev (code=exited, status=143)
 Main PID: 19146 (code=exited, status=143)

如果不加上SuccessExitStatus=143,stop服務的時候會變成failed狀態,而不是inactive狀態。

2、註冊開機自啓動:
systemctl enable myservice.service

如果修改服務配置文件,需要執行:
systemctl daemon-reload

3、查看該服務的console log:
journalctl -u myservice.service
journalctl -f -u myservice.service


發佈了201 篇原創文章 · 獲贊 397 · 訪問量 204萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章