In this article, we will take a look at how to deploy a Spring Boot application as a service in a Linux system. By deploying the application as a service, we enable the ability to re-start the Spring application on system reboot.
We have to follow a few steps to make the Spring Boot app executable. First, set the Spring Boot app's packaging to jar by changing the <packaging> tag to jar in pom.xml.
Next, we need to make the Spring Boot jar executable by setting executable to true in pom.xml. This will enable Linux to find the main class entry in the Spring Boot application to invoke
<configuration>
<executable>true</executable>
</configuration>
Build the application by running
This will create the jar file
Place this jar file in your Linux system. For this tutorial, we will assume the path to springboot.jar as
Create a file named
Include the below code in the springboot.service file.
[Unit]
Description=Springboot port 8090
After=syslog.target
[Service]
User=username
ExecStart=/software/java/openjdk17/jdk-17.0.1/bin/java -jar /software/tomcat/springboot.jar --server.port=8090
SuccessExitStatus=143
TimeoutStopSec=10
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
Make sure to set the correct path to ExecStart, your java installation directory, and the directory that contains the springboot.jar file.
The next step is to create a symlink for the service
You can delete an existing symlink with
Next, update the systemd by running
This will notify systemd about the new service
Now, enable service to run on system boot
Finally, start the service by running
Additionally, you can use the below commands on the service you created.