springboot 通过代码自动生成pid

发布于:2019-08-13 15:07:28

springboot 通过代码自动生成pid


package com.example.demo01.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.ApplicationPidFileWriter;

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
        //SpringApplication.run(DemoApplication.class, args);
        //在没有配置application.properties文件spring.pid.file=/var/log/app.pid时,默认保持在当前工程目录的application.pid文件中
        SpringApplication app = new SpringApplication(DemoApplication.class);
        app.addListeners(new ApplicationPidFileWriter());
        app.run(args);

}

}
application.properties文件spring.pid.file的配置key用于配置pid存放文件位置,启动服务后生成pid文件且内容时进程ID,服务停止时pid文件会被自动删除
有了pid文件后,停止服务: kill -9 $(cat /var/log/app.pid)   && rm -rf /var/log/app.pid