Reputation: 3273
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!
Upvotes: 12
Views: 25552
Reputation: 121
Using ApplicationPidFileWriter is one way as already mentioned by @dunni.
Another way would be consuming spring boot actuator. actuator/env will return all the environment details like port#, PID# and many other system properties in json format.
http://hostName:port#/actuator/env
To use this one must first enable and expose actuator in his application. Steps:
for details refer : https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html#production-ready-metrics-endpoint
Upvotes: 1
Reputation: 299
Just goto /var/run/{your app name}/ ,read the pid in the .pid file .
Upvotes: 0
Reputation: 1
No need to use ApplicationPidFileWriter, just use ApplicationPid:
SpringApplication springApplication = new SpringApplication(MyApplication.class);
springApplication.run(args);
log.info(new ApplicationPid().toString());
Upvotes: 0
Reputation: 131326
From the Part V. Spring Boot Actuator: Production-ready features documentation :
In the spring-boot module, you can find two classes to create files that are often useful for process monitoring:
- ApplicationPidFileWriter creates a file containing the application PID (by default, in the application directory with a file name of application.pid).
- WebServerPortFileWriter creates a file (or files) containing the ports of the running web server (by default, in the application directory with a file name of application.port).
By default, these writers are not activated, but you can enable:
- By Extending Configuration
- Section 60.2, “Programmatically”
Here is the 60.1 Extending Configuration part :
In the META-INF/spring.factories file, you can activate the listener(s) that writes a PID file, as shown in the following example:
org.springframework.context.ApplicationListener=\
org.springframework.boot.context.ApplicationPidFileWriter,\
org.springframework.boot.web.context.WebServerPortFileWriter
That enables both pid and port output at startup.
So the idea is rather simple : in the src/main/resources/META-INF
folder of your spring boot application, if not existing, create a spring.factories
file with the previous content to enable both (pid or port) or with the following to enable only the PID output :
org.springframework.context.ApplicationListener=org.springframework.boot.context.ApplicationPidFileWriter
Upvotes: 2
Reputation: 44515
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.
Upvotes: 24
Reputation: 489
You could execute the tasklist command to list the active processes and their identificators (PID) will appear.
You could also write them to a file with in the script:
tasklist /v txt > filename.txt
Afterwards, you can use the script to read the file and get the pid.
Eventually you would use the script to kill the process.
Upvotes: -1