Skip to content

Linux shell command to start, stop(kill) or restart springboot executable jar applications

1. The purpose of this post

Sometimes, we package Spring Boot applications as a single executable jar file and deploy it to a server. We start the jar as follows:

Terminal window
java -jar myapp-exec.jar

However, when we want to stop or restart it, we need to perform the following steps:

Terminal window
ps -ef|grep java # find the pid of the app
kill -9 <thepid>

This process can be cumbersome. Today, I will demonstrate Linux shell commands to kill or restart Spring Boot executable jar applications, allowing you to use a single command to manage your app.

2. Environments

  • Spring Boot 1.x or 2.x

3. The commands

3.1 Kill the Spring Boot App

If your Spring Boot jar file name is myapp-exec.jar, you can kill it using this command:

stop.sh
kill $(ps aux | grep 'myapp-exec.jar' | grep -v grep | awk '{print $2}')

Save the above script as stop.sh.

3.2 Start the Spring Boot App in the background

To start the app in the background, you can use the nohup command:

start.sh
nohup java -jar myapp-exec.jar > nohup.out &

Save the above script as start.sh.

3.3 Restart the Spring Boot App

Now, you can create a file named restart.sh to handle the restart process:

restart.sh
echo 'stop the app...'
./stop.sh
echo 'stopped'
sleep 2
echo 'start the app...'
./run.sh
echo 'started'

Save the above script as restart.sh.

Summary

This post provides a quick and efficient way to manage Spring Boot applications deployed as executable jar files on Linux servers. By using the provided shell scripts, you can easily start, stop, or restart your application with a single command. This approach saves time and reduces the complexity of managing Spring Boot applications in a production environment.

Final Words + More Resources

My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!