linux中添加service

出處:https://blog.csdn.net/ly690226302/article/details/79260875

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下任意目錄。

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章