3 min read

How to start and stop spring boot application using scripts?

January 16, 2018

Spring boot applications are easier to build and run. But there is one small problem. Most of the developers still struggle when it comes to running, stopping and starting the application in production servers. The traditional applications were deployed on a web container like tomcat which came with a startup.sh and shutdown.sh script. However this is not available out of the box with spring boot. This article helps you create a simple start and stop script for your spring boot web application.

startup.sh

The script is simple and straightforward.

#!/bin/bash
nohup java -jar /path/to/app/hello-world.jar > /path/to/log.txt 2>&1 &
echo $! > /path/to/app/pid.file

To break it down,

  • java -jar /path/to/app/hello-world.jar initializes the application.
  • > /path/to/log.txt redirects any anything written to STDOUT into the file /path/to/log.txt.
  • 2>&1 redirects all errors printed on STDERR to STDOUT. This way all the logs will go to STDOUT which is already redirected to /path/to/log.txt
  • & at the end makes the application run in the background.
  • echo $! will print the PID of the last command
  • The PID is then written to a file using > /path/to/app/pid.file
  • nohup lets the java process run in the background even after the user is logged out.

nohup may leave a nohup.out file in working directory when the STDOUT and STDERR is not redirected properly. If that’s not what you want, You may want to use disown command to kick-start the script. A sample use case would be disown startup.sh &. But I dislike the way of calling a script with another command.

shutdown.sh

Comparing to what we have written in the start script, shutdown.sh is pretty simple.

#!/bin/bash
kill $(cat /path/to/app/pid.file)

The shutdown script makes use of the PID file created by the startup script. This allows us to locate and kill the exact process we spawned earlier. The script is pretty simple and I believe this doesnt need any explanation.

restart.sh

I know that i’m overkilling the topic here. But for the sake of completeness, Here is your restart.sh.

#!/bin/bash
. /path/to/shutdown.sh
. /path/to/startup.sh

Here is something extra

You can restart your application straight from your local machines if you have a bash client using the following command. I personally use this method most of the time to restart my applications hosted on my servers.

sshpass -p password ssh userid@yourserver '/path/to/restart.sh'

Figure this one out yourself.

There are few points to note though.

  • Do not mix the name of the application jar files and paths. Always use canonical paths so its less confusing.
  • Do not mix the name of the PID files.
  • Your unix login may need necessary permissions to run few of the commands.
  • There may be parameters, active profiles and JVM arguments specific to your application. Make sure you configure them in the script.
Raja Anbazhagan

About the author

Raja is a Software Engineer with over 7 years of experience in working with Enterprise Java applications. Lately, He is focused in cloud-based Java applications and serverless technologies. He spends his spare time in stackoverflow.

Browse Categories