Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get process id of a spring boot application

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!

like image 291
walsh Avatar asked Nov 29 '16 10:11

walsh


People also ask

What is PID in spring boot?

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.

How do I get PID in Java?

getRuntimeMXBean(). getPid() method to get the process ID. This method returns the process ID representing the running Java virtual machine.

How do I check my application status in spring boot?

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.

What is spring boot actuator?

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.


2 Answers

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

Code

Result

like image 81
admin admin Avatar answered Sep 23 '22 03:09

admin admin


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.

like image 24
dunni Avatar answered Sep 22 '22 03:09

dunni