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

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
宝玉的分享
宝玉的分享
量子位
博客园 - 叶小钗
博客园_首页
Know Your Adversary
Know Your Adversary
S
Schneier on Security
罗磊的独立博客
C
Cyber Attacks, Cyber Crime and Cyber Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Simon Willison's Weblog
Simon Willison's Weblog
美团技术团队
WordPress大学
WordPress大学
大猫的无限游戏
大猫的无限游戏
Hacker News: Ask HN
Hacker News: Ask HN
Application and Cybersecurity Blog
Application and Cybersecurity Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Security Latest
Security Latest
月光博客
月光博客
Spread Privacy
Spread Privacy
C
Cybersecurity and Infrastructure Security Agency CISA
人人都是产品经理
人人都是产品经理
J
Java Code Geeks
C
CERT Recently Published Vulnerability Notes
Last Week in AI
Last Week in AI
Attack and Defense Labs
Attack and Defense Labs
NISL@THU
NISL@THU
H
Hacker News: Front Page
N
News and Events Feed by Topic
小众软件
小众软件
T
Threatpost
V2EX - 技术
V2EX - 技术
T
Tailwind CSS Blog
阮一峰的网络日志
阮一峰的网络日志
Project Zero
Project Zero
L
LINUX DO - 热门话题
Apple Machine Learning Research
Apple Machine Learning Research
C
CXSECURITY Database RSS Feed - CXSecurity.com
TaoSecurity Blog
TaoSecurity Blog
P
Privacy International News Feed
Latest news
Latest news
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
酷 壳 – CoolShell
酷 壳 – CoolShell
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
AWS News Blog
AWS News Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog

博客园 - _herbert

小程序拍证件并局部裁剪 信创-为什么ORACLE使用JDBC查询SYSDATE时,RS.getDate能获取到时间部分? linux宝塔面板使用API自动部署更新文件 JSON JAVA找出哪个类import了不存在的类 LocalDate,LocalDateTime,Date,日期串相互转换 PostMan加载三方JS PostMan内置对象 微信支付集成_JSAPI VUE中使用AXIOS包装API代理 spring中el表达式安全和扩展 Spring使用el表达式 springboot中File默认路径 ORACLE解析游标生成JSON ORACLE游标序列化 AI教我一条SQL实现明细转树形结构 Springboot构建包使用BOOT-INF中的class覆盖依赖包中的class Maven 构建知识库 MAVEN构建分离依赖JAR JAVA实现读取最后几行日志 信创-ORACLE迁移到KingbaseV9 信创-ORACLE迁移到DM8 sql分组 group by rollup,cube,grouping sets,group_id,groupingId
Springboot启动时记录进程ID
_herbert · 2025-11-15 · via 博客园 - _herbert

Springboot启动时记录进程ID

1. 背景说明

springboot项目打包成可执行jar包以后,需要通过java -jar xxx.jar启动项目.启动方式对非技术人员不太友好.所以需要项目构建时,生成一个start.batstop.bat的脚本.关闭采用taskkill -F -PID命令或者 kill -9 需要知道启动的进程ID.

2. 代码实现

  • springboot启动类,添加 ApplicationPidFileWriter listeners 实现启动时记录PID
// 小游戏 地心侠士
 SpringApplication appliction = new SpringApplication(AppsApplication.class);
 appliction.addListeners(new ApplicationPidFileWriter());
 appliction.run(args);

这样项目运行后,会在根目生成一个application.pid文件,记录启动的进程ID.

  • 实现关闭代码
// 小游戏  地心侠士
private static void shutdown() {
File file = new File("application.pid");
try {
	FileReader fileReader = new FileReader(file);
	String pid = null;
	try (BufferedReader br = new BufferedReader(fileReader)) {
		pid = br.readLine();
	}
	if (StringUtils.isNotBlank(pid)) {
		if (System.getProperty("os.name").toLowerCase().indexOf("windows") > -1) {
			Runtime.getRuntime().exec("taskkill -F -PID " + pid);
		} else {
			Runtime.getRuntime().exec("kill -9 " + pid);
		}
	}
} catch (IOException e) {
	if (e instanceof FileNotFoundException) {
		System.err.println("未找到文件:" + file.getAbsolutePath());
	}
	System.out.println("读取文件异常");
}
}
  • 启动类判断是关闭还是启动
public static void main(String[] args) throws IOException, ClassNotFoundException {
  for (String arg : args) {
		// 如果执行jar包时,参数为shutdown,则关闭项目
  	if ("shutdown".equals(arg)) {			
  		shutdown();
  		System.exit(0);
  	}
  }
	// TOOD 小游戏 地心侠士 
}

3. 构建配置

使用 maven-assembly-plugin 插件动态生成启动脚本,在fileSets指定文件夹路径,在package时,会自动替换其中的Maven变量,插件配置如下

<build><plugins><plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
      <appendAssemblyId>false</appendAssemblyId>
      <descriptors>
          <descriptor>./package.xml</descriptor>
      </descriptors>
  </configuration>
  <executions>
      <execution>
          <id>make-assembly</id>
          <phase>package</phase>
          <goals>
              <goal>single</goal>
          </goals>
      </execution>
  </executions>
</plugin></plugins></build>

package.xml 配置中,针对脚本处理配置如下,脚本存放在 /src/scripts 目录下

<fileSets><fileSet>
	<directory>src/scripts</directory>
	<outputDirectory>/</outputDirectory>
	<filtered>true</filtered>
</fileSet></fileSets>

4. 生成启动脚本

启动脚本模板,目录存放 /src/scripts/startup.bat, 使用maven打包变量可以生成具体启动脚本

@echo off
title SpringBoot-%date%-%time%-%cd%
java -jar -Dloader.path=resources,lib,plugin ${project.artifactId}-${project.version}.jar

5. 生成关闭脚本

关闭脚本模板,目录存放 /src/scripts/shutdown.bat

java -jar -Dloader.path=resources,lib ${project.artifactId}-${project.version}.jar shutdown

6. 总结

开发项目,尽量减少操作步骤.能代码化的脚本,一定代码化,减少人为出错的可能性.

原文地址:https://mp.weixin.qq.com/s/ZPyl-j9QgP-Pc6H-9dxFPQ