how to get process id of a spring boot application

Spring Boot provides the class ApplicationPidFileWriter, which will then write the PID into a file. You can activate it by adding it as a listener to the SpringApplication:

SpringApplication springApplication = new SpringApplication(DemoApplication.class);
springApplication.addListeners(new ApplicationPidFileWriter());
springApplication.run(args);

The constructor of ApplicationPidFileWriter can also take a String or a File object with a custom filename. Then you can read the PID from that file and use it in your scripts.


System.getProperty("PID") get SPRING BOOT PID.

Code

Result