惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
H
Hacker News: Front Page
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
Cisco Blogs
P
Palo Alto Networks Blog
Know Your Adversary
Know Your Adversary
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
AWS News Blog
AWS News Blog
Spread Privacy
Spread Privacy
S
Schneier on Security
The Hacker News
The Hacker News
Cyberwarzone
Cyberwarzone
T
Tenable Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
Tailwind CSS Blog
S
Secure Thoughts
N
Netflix TechBlog - Medium
T
The Exploit Database - CXSecurity.com
I
Intezer
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Help Net Security
Help Net Security
K
Kaspersky official blog
Google Online Security Blog
Google Online Security Blog
L
LangChain Blog
Martin Fowler
Martin Fowler
L
LINUX DO - 热门话题
Hacker News: Ask HN
Hacker News: Ask HN
www.infosecurity-magazine.com
www.infosecurity-magazine.com
有赞技术团队
有赞技术团队
P
Privacy International News Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Recent Announcements
Recent Announcements
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The Register - Security
The Register - Security
云风的 BLOG
云风的 BLOG
Google DeepMind News
Google DeepMind News
阮一峰的网络日志
阮一峰的网络日志
WordPress大学
WordPress大学
Recorded Future
Recorded Future
The Last Watchdog
The Last Watchdog
G
Google Developers Blog
T
Threatpost
小众软件
小众软件
S
Securelist
Recent Commits to openclaw:main
Recent Commits to openclaw:main
O
OpenAI News

博客园 - 黄洪波

安装openclaw 排查项目中依赖的mybatis 拦截器 ModelAttribute 老革命遇上新问题 使用calcite构造ddl建表语句 OpenWebUI单点登录之解决动态参数问题 IDEA自带的Maven 3.9.x无法刷新http nexus私服(转) windows docker安装rocketmq之踩坑记 分布式系统设计经典论文(转载) 训练自己的yolo-v11数据集(二) 训练自己的yolo-v11数据集(一) 本地使用pycharm进行yolo推理 2024年的云原生架构需要哪些技术栈 (转) yolo v11学习,入门篇 OpenWebUI单点登录之深坑 AI工具验证 解决Win10无法进入睡眠模式(转) idea常用插件 内网离线模式下激活JRebel java导入json数据至doris
将SpringBoot打包之后的jar设为守护进程
黄洪波 · 2024-08-28 · via 博客园 - 黄洪波

要在Linux系统上将Spring Boot打包的jar服务设置为守护进程,并实现服务挂掉后自动重启,你可以使用systemd或supervisord这样的工具。

我选择了systemd的方案

最终脚本如下:

创建一个脚本 /home/beirui/start-beirui-admin.sh,内容如下:

#!/bin/bash
/usr/bin/java -jar /home/beirui/beirui-admin.jar > /home/beirui/beiruiAdminLog.txt 2>&1

然后给这个脚本赋予执行权限:

sudo chmod +x /home/beirui/start-beirui-admin.sh

创建服务单元文件如下

1. 创建一个systemd服务文件

首先,创建一个systemd服务文件,例如my-springboot-app.service

sudo nano /etc/systemd/system/my-springboot-app.service

2. 编辑服务文件

在文件中添加以下内容,替换占位符为你的实际信息:[Unit]

Description=Beirui Sap Qimen Spring Boot Application
After=network.target


[Service]
User=root
# 设置工作目录为你的Spring Boot应用所在目录 WorkingDirectory
=/home/beirui ExecStart=/home/beirui/start-beirui-admin.sh
#在某些情况下,使用重定向符号(>)在 ExecStart 中可能无法正常工作。你可以尝试将日志重定向到文件的操作移动到一个脚本中,然后在 ExecStart 中调用这个脚本。
#使用下面两种方式,都没有实现将日志输出到 /home/beirui/beiruiAdminLog.txt中,故采取脚本调用的方式
#只能通过journalctl -u beirui-admin.service -f查看日志
#ExecStart
=/usr/bin/java -jar /home/beirui/beirui-admin.jar >> /home/beirui/beiruiAdminLog.txt 2>&1 #ExecStart=/usr/bin/java -jar /home/beirui/beirui-admin.jar SuccessExitStatus=143 Restart=always RestartSec=10 #StandardOutput=file:/home/beirui/beiruiAdminLog.txt #StandardError=file:/home/beirui/beiruiAdminLog.txt [Install] WantedBy=multi-user.target

3. 加载服务文件并启动服务

运行以下命令以重新加载systemd,启用并启动你的Spring Boot服务:

sudo systemctl daemon-reload
sudo systemctl enable my-springboot-app.service
sudo systemctl start my-springboot-app.service

4. 检查服务状态

你可以用以下命令检查服务是否在运行:

sudo systemctl status my-springboot-app.service

5. 自动重启服务

Restart=always这一行会确保当服务挂掉时,它会自动重启。

你可以根据需要调整RestartSec的时间来设定重启的延迟时间。

通过这些步骤,你的Spring Boot服务将作为守护进程运行,并在崩溃或停止后自动重启。

在某些情况下,需要停止该服务,如何拒绝自动重启服务呢

要在特定情况下停止Spring Boot服务,并且不希望它自动重启,你可以使用以下方法:

方法 1: 手动停止服务并禁用自动重启

你可以通过以下命令停止服务并同时禁用自动重启功能:

sudo systemctl stop my-springboot-app.service
sudo systemctl disable my-springboot-app.service

disable命令会防止服务在系统启动时自动启动,也会避免服务在手动停止后自动重启。

方法 2: 暂时停止服务不自动重启

如果你希望在停止服务时暂时禁止自动重启,但不想完全禁用服务,可以先使用以下命令:

如果你希望在停止服务时暂时禁止自动重启,但不想完全禁用服务,可以先使用以下命令:

sudo systemctl stop my-springboot-app.service

然后临时将服务的Restart策略改为no

sudo systemctl edit my-springboot-app.service

添加以下内容:

保存并退出。这样,服务将不会自动重启。

要恢复原来的自动重启策略,可以删除这些临时修改:

sudo systemctl revert my-springboot-app.service

方法 3: 使用systemctl mask命令

你也可以使用mask命令来彻底防止服务启动:

sudo systemctl mask my-springboot-app.service

mask会将服务链接到/dev/null,从而彻底防止其启动。要解除这个状态,可以使用:

sudo systemctl unmask my-springboot-app.service

这些方法可以根据不同的需求选择。如果你只是想停止服务并防止其自动重启,方法1和2是最直接的选择。