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

image.png

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

你可能感兴趣的:(springboot 通过代码自动生成pid)