A logo showing the text blog.marcnuri.com
Español
Home»Java»Spring Boot as a Linux service

Recent Posts

  • Fabric8 Kubernetes Client 7.4 is now available!
  • Kubernetes MCP Server Joins the Containers Organization!
  • MCP Tool Annotations: Adding Metadata and Context to Your AI Tools
  • Fabric8 Kubernetes Client 7.2 is now available!
  • Connecting to an MCP Server from JavaScript using AI SDK

Categories

  • Artificial Intelligence
  • Front-end
  • Go
  • Industry and business
  • Java
  • JavaScript
  • Legacy
  • Operations
  • Personal
  • Pet projects
  • Tools

Archives

  • September 2025
  • July 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • August 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • February 2020
  • January 2020
  • December 2019
  • October 2019
  • September 2019
  • July 2019
  • March 2019
  • November 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • December 2017
  • October 2017
  • August 2017
  • July 2017
  • January 2017
  • December 2015
  • November 2015
  • December 2014
  • March 2014
  • February 2011
  • November 2008
  • June 2008
  • May 2008
  • April 2008
  • January 2008
  • November 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007

Spring Boot as a Linux service

2017-10-02 in Java / Operations tagged Daemon / Linux / Service / Spring Framework / Spring Boot / Systemctl / systemd by Marc Nuri | Last updated: 2025-09-12
Versión en Español

Introduction

In this post, I'll show you how to configure a Spring Boot application to run as a service on Linux.

First, I'll show you how to configure the service with scripts compatible with System V init systems. Then, I'll explain how to configure the service on newer systems that use systemd.

Executable Jar

One of the advantages of Spring Boot is that it allows us to create self-contained executable Jars, which lets us treat the packaged application in a Jar directly as an executable that provides parameters like "start", "stop", "restart", "status" that are useful when managing services.

To create an executable Jar, we need to do one of the following two configurations depending on whether we use Maven or Gradle:

Maven

Add the following to the project's pom.xml:

pom.xml
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <executable>true</executable>
    </configuration>
</plugin>

Gradle

Add the following to the project's build.gradle:

build.gradle
springBoot {
    executable = true
}

Starting the application from the Jar, in addition to supporting the previously mentioned options, implies the following:

  • The script starts the application with the user who owns the script
  • The script tracks the application's PID using /var/run/<applicationname>/<applicationname>.pid
  • The script writes logs to /var/log/<applicationname>.log

Installation as init.d service (System V)

For this case, we'll leverage the previously mentioned properties of executable Jars that can be created in Spring Boot.

In this case, the task will be as simple as adding a symbolic link to the Jar in the init.d directory:

bash
sudo ln -s /var/<applicationpath>/<applicationname>.jar /etc/init.d/<applicationname>

Once this is done, you can start/stop/restart the application exactly as with any other service:

bash
service <applicationname> start

Similarly, we can mark the application (service) to start automatically when the system boots:

bash
update-rc.d <applicationname> defaults

Securing the service

As I've indicated, the init.d service will start the application with the user who owns the application. In this regard, it's important that this user has the necessary permissions for the application to behave correctly.

The user used to start the application should be one without login capability. We can use the following command to modify the shell and prevent login for the user:

bash
chsh -s /usr/sbin/nologin <user>

We can change the application's owner user as follows:

bash
chown <user>:<group> <applicationname>.jar

The application jar should only have read and execute permissions for its owner, so we change its attributes:

bash
chmod 500 <applicationname>.jar

In the end, we should have a structure like the following:

Spring application properties as service
Spring application properties as service

Installation as systemd service

Nowadays, most Linux distributions use systemd to manage system processes. Unlike SystemV where startup parameters are by convention, in the case of systemd it's by configuration, so to enable a service it's necessary to create a configuration script.

Configuration script

Scripts are located in the /etc/systemd/service directory, so for our application we'll need to create a file called /etc/systemd/service/<applicationname>.service like the following:

<applicationname>.service
[Unit]
Description=<applicationname>
After=syslog.target

[Service]
User=<executoruser>
ExecStart=/path/to/application/<applicationname>.jar
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target

To mark the service to start automatically when the system boots, we'll use the following command:

bash
sudo systemctl enable applicationname.service

Conclusion

In this post I've shown you how to configure a Spring Boot application to run as a service on Linux. I've shown two methods, one for older systems that use System V and another for newer systems that use systemd.

References

  • https://stackoverflow.com/questions/21503883/spring-boot-application-as-a-service
  • https://docs.spring.io/spring-boot/docs/current/reference/html/deployment.html#deployment-initd-service
  • https://www.freedesktop.org/software/systemd/man/systemd.service.html
Spring boot as a Linux service
Spring boot as a Linux service
Twitter iconFacebook iconLinkedIn iconPinterest iconEmail icon

Post navigation
Docker container as a Linux system serviceSpring-data + Mongo: Auto-increment sequence in MongoDB using a Spring service
© 2007 - 2025 Marc Nuri