環境
SpringBoot 2.X
CentOS7
問題
springboot應用一般都打包成一個單獨的jar包,然後使用nohup java -jar xxxx.jar &
,但是這樣有個問題:服務器重啓後需要手工啓動。
解決
後來翻官方網站,找到辦法:
官方地址
我這裏使用3.2.1 init.d 方法。
#使用軟連接方式,不需要寫啓動腳本
$ sudo ln -s /var/myapp/myapp.jar /etc/init.d/myapp
#開啓啓動
chkconfig myapp on
#啓動
service myapp start
#停止
service myapp stop
#重啓
service myapp restart
#查看狀態
service myapp status
注:
cannot execute binary file
:必須使用spring boot的Maven插件來打包,並且設置爲可執行;
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
- 控制檯日誌輸出至 /var/log/myapp.log
- 如果出現 Permission denied,給myapp增加執行權限
chmod +x /var/myapp/myapp.jar