I noticed spring boot printed the process id in the log during it's startup. Now I want write a script to kill this process using this pid and start the application again. Does Spring Boot provide any api to get this pid? Thanks!
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.
this is not working for me. Log is also written to pid file. Have you come across such a situation?
Create a new question and provide the relevant details (code snippets etc.).
To add some more detail to the answer, you can configure the location of this file by providing the --spring.pidfile parameter when launching your application