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

 

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