In this post, We will learn about start and stop shell scripts to run Spring Boot applications.
Spring boot applications are easier to build and run. However, 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
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.
The startup script for Spring Boot is straightforward. Let’s take our hello-world application.
echo $! > /path/to/app/pid.filenohup java -jar /path/to/app/hello-world.jar > /path/to/log.txt 2>&1 & Code language: Bash (bash)
To break it down,
java -jar /path/to/app/hello-world.jarinitializes the application.
> /path/to/log.txtredirects any anything written to
STDOUTinto the file
2>&1redirects all errors printed on
STDOUT. This way all the logs will go to
STDOUTwhich is already redirected to
&at the end makes the application run in the background.
echo $!will print the
PIDof the last command
- The PID is then written to a file using
nohuplets the java process run in the background even after the user is logged out.
nohupmay leave a
nohup.outfile in working directory when the
STDERRis not redirected properly. If that’s not what you want, You may want to use
disowncommand 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.
Comparing to what we have written in the start script, the stop script is pretty simple.
kill $(cat /path/to/app/pid.file)Code language: Bash (bash)
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 doesn’t need any explanation.
I know that I’m overkilling the topic here. But for the sake of completeness, Here is your
. /path/to/shutdown.sh . /path/to/startup.shCode language: Bash (bash)
You can stop and start 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'Code language: Bash (bash)
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
- Your unix login may need necessary permissions to run few of the commands.
- There may be
JVM argumentsspecific to your application. Make sure you configure them in the script.
To conclude, We learned how to write startup.sh, shutdown.sh, and restart.sh to manage the spring boot application process. If you feel like it, you can learn more about converting the application into a WAR file so that you can manage the application via the application server itself.