Linux下運行的軟件咱們一般把他註冊爲服務,這樣咱們就能夠經過命令開啓、關閉以及保持開機啓動等功能。
若想使用此項功能,咱們須要將代碼中關於spring-boot-maven-plugin的配置修改成:
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
而後使用mvn package 打包。
主流的Linux大多使用init.d或systemd來註冊服務。下面以CentOS6.6演示init.d註冊服務;以CentOs7.1演示systemd註冊服務。
(1)安裝JDK
從oracle官網下載JDK,注意選擇的是:jdk-8u51-linux-x64.rpm。這是紅帽系linux專用安裝包格式,將jdk下載放置到linux下任意目錄。
執行下面命令安裝JDK:
rpm -ivh jdk-8u51-linux-x64.rpm
(2)基於Linux的int.d部署
註冊服務,在centOS6終端執行:
sudo ln -s /var/apps/test-0.0.1-SNAPSHOT.jar /etc/init.d/test
其中test是咱們的服務名。
啓動服務:
service test start
中止服務:
service test stop
服務狀態:
service test status
開機啓動:
chkconfig test on
項目日誌存放於/var/log/test.log下,能夠用cat或tail等命令查看。
(3)基於Linux的Systemd部署
在/etc/systemd/system/目錄下新建文件test.service,填入下面內容:
[Unit]
Description=test
After=syslog.target
[Service]
ExecStart= /usr/bin/java -jar /var/apps/test-0.0.1-SNAPSHOT.jar
[Install]
WantedBy=multi-user.target
注意,在實際使用中修改Description和ExecStart後面的內容
啓動服務:
systemctl start test
或systemctl start test.service
中止服務:
systemctl stop test
或systemctl stop test.service
服務狀態:
systemctl status test
或systemctl status test.service
開機啓動:
systemctl enable test
或systemctl enable test.service
項目日誌:
journalctl -u test
或 journalctl -u test.service
原文:https://blog.csdn.net/ly690226302/article/details/79260875
版權聲明:本文爲博主原創文章,轉載請附上博文連接!java