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!
There is a useful class in Spring Boot called ApplicationPidFileWriter. This class is used for saving an application PID into a file. PID is a unique identifier of the an active process in a operation system.
getRuntimeMXBean(). getPid() method to get the process ID. This method returns the process ID representing the running Java virtual machine.
For running the Spring Boot application, open the main application file, and run it as Java Application. When the application runs successfully, it shows the message in the console, as shown below. Now, open the browser and invoke the URL http://localhost:8080.
Spring Boot's 'Actuator' dependency is used to monitor and manage the Spring web application. We can use it to monitor and manage the application with the help of HTTP endpoints or with the JMX.
System.getProperty("PID")
get SPRING BOOT PID.
Code
Result
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With