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
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!