软件编程
位置:首页>> 软件编程>> java编程>> springboot 通过代码自动生成pid的方法

springboot 通过代码自动生成pid的方法

作者:神易风  发布时间:2023-11-27 02:54:57 

标签:springboot,pid

springboot项目部署

平时我们在部署springboot打成jar方式部署得时候,大多数都会编写启动脚本,脚本有很多种写法,但大多数意思都是一样的,java -jar 启动项目,获取进程pid保存到指定文件中。关闭程序时,获取进程pid kill -9 $pid。获取pid有很多种写法,简答粗暴netstat -nlp port | grep port | grep -v 。其实springboot本身就有更简单方式来处理这种问题,两行代码就搞定。


@SpringBootApplication
public class PidApplication {

public static void main(String[] args) {
   SpringApplication app = new SpringApplication(PidApplication.class);
   app.addListeners(new ApplicationPidFileWriter());
   app.run(args);
 }
}

启动项目后会在生成application.pid文件存放pid

springboot 通过代码自动生成pid的方法

如果你想存放指定目录在配置中添加spring.pid.file=/var/log/app.pid 即可是不是很简单啊

来源:https://www.jianshu.com/p/9a6f8a4d8968

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com