linux把可运行的jar包注册为服务并开机启动


最近在做项目的时候,开发完成后需要将打包的可运行jar包注册为linux系统服务,通过服务启停命令来控制jar的部署。

一、编写jar启动执行的脚本

vim new_monitor-start.sh
脚本:

#!/bin/sh

nohup /usr/local/java/jdk1.8.0_171/bin/java -jar /home/new_monitor/jar/monitor/new_monitor.jar > /home/new_monitor/jar/monitor/new_monitor.log &
echo $! > /var/run/new_monitor.pid

二、编写jar停止执行的脚本

vim new_monitor-stop.sh

#!/bin/bash
PID=$(cat /var/run/new_monitor.pid)
kill -9 $PID

三、在/usr/lib/systemd/system下编写开机自启脚本

cd /usr/lib/systemd/system
vim new_monitor.service

加入如下内容:

[Unit]
Description=new_monitor_service
After=syslog.target network.target

[Service]
Type=forking
ExecStart=/home/new_monitor/new_monitor-start.sh
ExecStop=/home/new_monitor/new_monitor-stop.sh
PrivateTmp=true

[Install]
WantedBy=multi-user.target

四、启动、停止、开机自启动

systemctl start/stop/enable new_monitor

转载自:https://blog.csdn.net/xiaoxiangzi520/article/details/90602743


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM