1. 程式人生 > >springboot可執行jar包開機啟動

springboot可執行jar包開機啟動

官方教程

https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html

在這裡插入圖片描述

設定開機啟動

[[email protected] ~]$ sudo ln -s /home/seth/blog-0.0.1-SNAPSHOT.jar /etc/init.d/blog
[[email protected] ~]$ sudo chkconfig blog on
[[email protected] ~]$ sudo service blog start
Started [3652]
[
[email protected]
~]$ sudo service blog status Running [3652]

reboot重啟驗證開機啟動

[[email protected] ~]$ sudo reboot
Connection to 35.241.117.72 closed by remote host.
Connection to 35.241.117.72 closed.
ERROR: (gcloud.compute.ssh) [/usr/bin/ssh] exited with return code [255].
sethmbp:~ seth$ gcloud compute --project "blog-222412" ssh --zone "asia-east2-a" "instance-1"
Last login: Fri Dec 21 09:34:16 2018
[
[email protected]
~]$ sudo service blog status Running [2940]

移除開機啟動

只在需要移除的時候操作,不要跟著做了

[[email protected] ~]$ sudo service blog stop
Stopped [2940]
[[email protected] ~]$ sudo chkconfig blog off
[[email protected] ~]$ sudo rm /etc/init.d/blog 

跟啟動引數

畢竟是個線上專案,引數還是要給的,先上圖
這個可能有些人不太理解,為什麼要跟個conf,其實官方規定的
先上個連結

https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html
在這裡插入圖片描述

先證明以下不是我隨便說的

在這裡插入圖片描述

[[email protected] ~]$ ll
總用量 39060
-rw-rw-r--. 1 seth seth      321 12月 21 09:49 blog-0.0.1-SNAPSHOT.conf
-rwxr-xr-x. 1 seth seth 17592218 12月 21 09:14 blog-0.0.1-SNAPSHOT.jar
[[email protected] ~]$ cat blog-0.0.1-SNAPSHOT.conf
APP_NAME=seth-blog
MODE=service #後臺執行
JAVA_OPTS="-server -Xmx8g -Xms4g -Xloggc:/tmp/logs/pandora_gc.log -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=utf-8"
RUN_ARGS="--server.port=3011 --spring.profiles.active=debug --spring.pid.file=/tmp/logs/blog.pid"
LOG_FOLDER=/tmp/logs
LOG_NAME=seth-blog
[[email protected] ~]$ sudo service blog restart
Stopped [3982]
Started [4084]
[[email protected] ~]$ ps -ef | grep blog
seth      4084     1 99 09:50 ?        00:00:05 /bin/java -Dsun.misc.URLClassPath.disableJarChecking=true -server -Xmx8g -Xms4g -Xloggc:/tmp/logs/pandora_gc.log -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=utf-8 -jar /home/seth/blog-0.0.1-SNAPSHOT.jar --server.port=3011 --spring.profiles.active=debug --spring.pid.file=/tmp/logs/blog.pid
seth      4098  3243  0 09:50 pts/0    00:00:00 grep --color=auto blog
[[email protected] ~]$ ll /tmp/logs/
總用量 28
-rw-r--r--. 1 seth root 22590 12月 21 09:50 blog.log
-rw-r--r--. 1 seth seth   516 12月 21 09:50 pandora_gc.log

blog-0.0.1-SNAPSHOT.conf

注意:conf的檔名和jar包名保持一致,存放到同一個目錄,

注意一下日誌和pid的存放目錄,這裡我用的tmp,生產環境絕對不能用tmp,尤其是pid,tmp下系統會定期清理的
到時候無法stop了

APP_NAME=seth-blog  #隨便寫
MODE=service #後臺執行
JAVA_OPTS="-server -Xmx8g -Xms4g -Xloggc:/tmp/logs/pandora_gc.log -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=utf-8"
RUN_ARGS="--server.port=3011 --spring.profiles.active=debug --spring.pid.file=/tmp/logs/blog.pid"
LOG_FOLDER=/tmp/logs
LOG_NAME=seth-blog