How to get spring boot application process id

I noticed that spring loaded the process id in the log when it started. Now I want to write a script to kill this process with this pid and run the application again. Does spring boot load any api to get this pid? Thanks!

+8
source share
4 answers

Spring Boot provides the ApplicationPidFileWriter class, which then writes the PID to the file. You can activate it by adding it as a SpringApplication listener:

 SpringApplication springApplication = new SpringApplication(DemoApplication.class); springApplication.addListeners(new ApplicationPidFileWriter()); springApplication.run(args); 

The ApplicationPidFileWriter constructor can also accept a String or File object with a custom file name. Then you can read the PID from this file and use it in your scripts.

+16
source

You can run the tasklist command to display the active processes and their identifiers (PIDs) will appear.

You can also write them to a file using a script:

 tasklist /v txt > filename.txt 

Then you can use a script to read the file and get the pid.

In the end, you can use a script to kill the process.

0
source

I tried below: -

 SpringApplication application=new SpringApplication(StartofdayApplication.class); application.addListeners(new ApplicationPidFileWriter(new File("C:\\temp\\StartofdayApplication.pid"))); application.run(args); 

But getting a warning, because ApplicationPidFileWriter is deprecated, my spring version is 1.4.7, is there an alternative way to achieve the same.

0
source

From part V. Spring loading drive: Documentation for ready-made parts :

In the spring-boot module, you can find two classes for creating files, which are often useful for monitoring processes:

  • ApplicationPidFileWriter creates a file containing the application PID (by default, in the application directory with the file name application.pid).
  • WebServerPortFileWriter creates a file (or files) containing the ports of a running web server (by default, in the application directory with the file name application.port).

By default, these writers are not activated, but you can enable:

  • Expanding Configuration
  • Section 60.2, "Software"

Here is part of the 60.1 configuration extension:

In the META-INF / spring.factories file, you can activate listeners that write the PID file, as shown in the following example:

 org.springframework.context.ApplicationListener=\ org.springframework.boot.context.ApplicationPidFileWriter,\ org.springframework.boot.web.context.WebServerPortFileWriter 

This allows you to display both pid and port at startup.

So the idea is quite simple: in the src/main/resources/META-INF folder of your spring.factories boot application, if it does not exist, create a spring.factories file with the previous contents to include both (pid or port) or with The following is to enable only PID output:

 org.springframework.context.ApplicationListener=org.springframework.boot.context.ApplicationPidFileWriter 
0
source

Source: https://habr.com/ru/post/1012721/


All Articles