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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值