Gracefully Shutdown Spring Boot Application from Shell Script
<p>I have a spring boot application which I run using an executable jar file. Currently to stop the service we are just killing the process. I saw that we can use the following methods to shutdown the application gracefully. </p>

<pre><code>ApplicationContext ctx = SpringApplication.run(Application.class, args);
</code></pre>

<p>and then somewhere in the code I can call <code>ctx.close()</code> Or we can use the following static method.</p>

<pre><code>SpringApplication.exit(ApplicationContext, ExitCodeGenerator)
</code></pre>

<p>It works for us currently, but we are actually calling this <code>ctx.close()</code> method inside a controller as follows.</p>

<pre><code>@RequestMapping("/shutdownSpringBoot")
public void shutdownApplication() {
    MyApplication.ctx.close(); // I'm saving the context returned by spring boot in a static variable inside my main class
}</code></pre>

<p>When we hit this controller method via http the application is gracefully shutdown. But we dont want to do it this way. Is it possible to write a shell / batch script to trigger the java class inside which I can call the <code>ctx.close()</code> method ? We are looking for a shutdown script like the one we get from a standalone tomcat container (shutdown.bat / shutdown.sh), so that we can give our application as a jar file to our customers and they can start or stop the application by executing those scripts. (Which they are used to).</p>

<p>Thanks,
Sanjay</p>