How to Persist Spring Boot Application PID to a File (Step‑by‑Step Guide)
This tutorial explains why persisting a Spring Boot application's Process ID (PID) is useful, how to use the built‑in ApplicationPidFileWriter to write the PID to a file, and how to customize the file location via configuration properties.
1. Introduction
Welcome to the Spring Boot 2 practical series. The PID (Process ID) is a unique identifier assigned by the operating system to each process. It is used in system operations such as kill -9 <pid>.
2. Spring Boot Application Process
The jps command lists all Java processes, their main class or JAR name, and JVM arguments. A Spring Boot application runs as a Java process and therefore has a PID, which can be seen in the startup log, e.g.:
2019-11-20 14:28:00.925 INFO 7828 --- [ main] c.f.s.s.SecurityLearningApplication : Starting SecurityLearningApplication on DESKTOP-L0IOI2S with PID 7828When multiple Spring Boot applications run, distinguishing their PIDs becomes difficult, so persisting the PID to a file simplifies management.
3. Writing Spring Boot PID to a File
Spring Boot provides ApplicationPidFileWriter to write the PID to a file (default application.pid) at startup. If writing fails, the PID is stored in the system property PID_FAIL_ON_WRITE_ERROR or the Spring environment property spring.pid.fail-on-write-error.
3.1 Configuring PID Persistence
By default ApplicationPidFileWriter is not auto‑configured; you must register it manually in the main class:
package cn.felord.spring.security;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.ApplicationPidFileWriter;
import org.springframework.cache.annotation.EnableCaching;
/**
* @author Felordcn
*/
@SpringBootApplication
public class SecurityLearningApplication {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(SecurityLearningApplication.class);
springApplication.addListeners(new ApplicationPidFileWriter());
springApplication.run(args);
}
}After this configuration, the application creates application.pid containing the PID. You can customize the file name and location with the property spring.pid.file:
spring:
pid:
file: /var/run/myApp.pidRestarting the application will generate /var/run/myApp.pid with the current PID.
4. Conclusion
This article explained how to persist a Spring Boot application's PID to a file using programmatic configuration. You can customize the storage file path and name as needed.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
