【设置Java服务开机自启动】

设置Java服务开机自启动

方法一
新建Jar包运行脚本my_project.sh,路径根据实际情况修改:

#!/bin/bash

nohup java -jar /root/my_project/my_project-1.0.0.jar --spring.config.location=/root/my_project/application-dev.yml >> /root/my_project/log.out 2>&1 &

修改/etc/rc.local文件,文件末尾添加/root/my_project/my_project.sh &

#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local

/root/my_project/my_project.sh &

chsm-host.shrc.local设为可执行文件:

chmod +x /root/my_project/my_project.sh
chmod +x /etc/rc.d/rc.local

重启服务器查看Java服务是否重启

🏆方法二

一、编写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
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yohann*

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值