This tutorial is going to cover how to install a Spring Boot application as a Linux service, which enables us to start, stop and check application status using standard commands such as: service start payment-service, service stop payment-service and service status payment-service.

1. Build Spring Boot Applications To Executable Jar or War File

Before we install a Spring Boot application as a Linux service, we would need to build it into an full executable jar or war file. And to do that, we can use the following plugin configurations.

If it’s already there in the pom.xml or gradle.build files, we can skip this step.

With Maven:

With Gradle:

To create fully executable jar or war files, we can simply execute the following command:

See more detail how to run a Spring Boot application.

2. Install Executable Jar or War File As A Linux Service

Assuming we have a Spring Boot application and we’re going to install the application as a Linux service. The directory structure under /opt/apps is organized as below:

Install Spring Boot application as a Linux service - Payment-Service directory structure
Directory structure of payment-service under /opt/apps

There would be 3 files in the directory:

  • payment-service-1.0.jar: an executable Jar or war file of the payment service, which is created as described above.
  • payment-service-1.0.conf: a configuration file where holds additional configurations and parameters for the application such as xmx, xms and spring.profiles.active. Several characteristics of the file are as below:
    • The name of configuration file, which should be the same with the jar or war file except the extension, .conf instead of .jar or .war.
    • The file should be placed in the same directory with the Jar file.
    • If there is no such parameters, we don’t need to create the file.
  • payment-service.service: the file describes required configurations for a Linux Systemd service.

And now let get to the detail and actions with those files:

2.1. payment-service-1.0.jar

At first, we would need to

  • Copy the file to the /opt/apps/payment-service directory.
  • Add executable permission for the file, for example:

2.2. payment-service-1.0.conf

The next step is to create a payment-service-1.0.conf file at /opt/apps/payment-service directory with the following content:

Where:

  • xmx: 1GB, maximum memory allocation pool for a Java virtual machine (JVM).
  • xms: 1G, the initial memory allocation pool.
  • spring.profiles.active: dev, the Spring Boot active profiles
  • server.port: 8080, the port on which the application will be listening.

2.3. payment-service.service

Finally, let’s create a payment-service.service file at the same directory /opt/apps/payment-service with the content.

Note that:

  • Parameter User: vagrant, the system user will run the payment-service.
  • Parameter ExecStart: the path to our executable Jar or War file.

Then move the file to /etc/systemd/system/ directory, or we can create the file directly in the directly.

3. Operating The Application On Linux

3.1. To start the payment-service application

3.2. To stop the application

3.3. To check the status of application

3.4. To upgrade the service

  • Stop the service
  • Copy the new Jar file or War file to the /opt/apps
  • Start the service

4. Troubleshooting

Sometimes, it’s difficult to check if the application has been started successfully or not, below are some basic steps we can consider:

  • To check the application log file
  • To use other Linux commands to make sure application started or not, or example, ps -ef | grep java.

5. Conclusion

We have just gotten through how to install a Spring Boot application as a Linux service with Systemd. An alternative approach, which will be covered in another tutorial, is to install Spring Boot applications as an init.d service (System V).

5 1 vote
Article Rating