7

I’m a spring-boot newbie, so please go easy on me.

I need to offer a way for an administrator to start and stop my spring-boot microservice from a job scheduler. If I can create start.bat and stop.bat files for the service, then the scheduler could call them.

How do I stop a spring-boot microservice from command line without killing the process? I'd like a graceful exit, if possible.

The host will be a Windows server.

Termininja
  • 6,620
  • 12
  • 48
  • 49
Eric
  • 143
  • 1
  • 3
  • 9

4 Answers4

6

If you have Spring Boot Actuator included in your project, you can enable a shutdown endpoint (by default it is not enabled). This means that if you make a request to: http://yourserver.com/yourapp/shutdown, the application will shutdown gracefully. An administrator could do such a request using a standard tool such as curl.

See Endpoints in the Spring Boot reference documentation. You can enable the shutdown endpoint by adding the following to your application.properties:

endpoints.shutdown.enabled=true

Ofcourse, you'll want to restrict access to this endpoint, otherwise anyone who has access to the service could do a request and shutdown the application.

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • 1
    And then can I use spring security to restrict who can call the shutdown? Again, I'm a newbie. – Eric Jul 15 '16 at 16:19
  • 2
    Yes, and as you can see in the documentation, the `shutdown` endpoint is set to "sensitive" by default, which means that it will require a username / password if security is enabled in the app. The Spring reference docs contain a lot of info on this, and there are also tutorials like [this one](https://spring.io/guides/gs/securing-web/). – Jesper Jul 15 '16 at 17:09
  • Thank you very much. I really appreciate it! – Eric Jul 15 '16 at 17:57
  • Note that the shutdown request has to be sent as a POST request. GET doesn't work. – Eric Jul 27 '16 at 20:48
3

In cmd:

netstat -ano | findstr :[port_on_which_app_runs]

then

taskkill /PID [PID_of_the_app] /F

Found here

Kaisumaro
  • 31
  • 2
1

The easiest way to kill a process using cmd is by typing this single line of command:

> kill $(lsof -t -i:port_num)
webcrawler
  • 79
  • 1
  • 5
0

Starting spring version 2.3 there is support for graceful shutdown via spring property

server.shutdown=graceful

for new incoming requests the server will immediately issue 503 "Service unavailable" if any ongoing requests is yet to be processed it will for default time which we can change via below property

spring.lifecycle.timeout-per-shutdown-phase=1m