4 min read

How install spring boot application as a linux service?

January 18, 2018

Spring boot has a built in support for running the applications a windows or linux service. In this tutorial you will learn how to install a spring boot application as a linux service.

Before getting started

You will need,

Step 1: Make the jar an executable

Spring boot applications can be started using the command java -jar hello-world.jar. We have seen this behaviour in the posts How to start and stop spring boot application using scripts? and Spring boot hello world web application. But it can also be started as an independent executable by doing a simple tweak in pom.xml.

For this change your pom.xml to have to following configuration entry.

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

After running mvn package, you can simply run the JAR file by calling ./hello-world-0.0.1-SNAPSHOT.jar. The trick here is that spring-boot-maven-plugin adds a small snippet of code so the system that receives the above command treats them as an executable when the configuration.executable flag is set to TRUE.

Note that some Operating systems like FreeBSD and OSX may fully not support this feature.

Step 2: Configure init.d service

This is fairly simple. Once you have the executable jar file, You can symlink it to /etc/init.d directory where all services are usually configured in linux systems.

Assuming you have your executable jar file in the path /var/applications/hello-world.jar, run the following command to create a symlink under init.d.

sudo ln -s /var/applications/hello-world.jar /etc/init.d/hello-service

If everything went right, you can start the service using the following command.

sudo service hello-service start

With the above setup,

  • You can start, stop, restart and check status of the service.
  • The current PID of the service will be captured in /var/run/hello-service/hello-service.pid.

    • This ID can be used later to kill the process, But there is already a stop command argument for that.
    • stop command will try to kill the process gracefully with a SIGTERM. If the process doesnt stop within 60 seconds, it will be force killed. And this timeout is configurable.
  • The service will be started as the user who owns the jar file.
  • If not configured with a logger, the console logs will be written automatically to /var/log/hello-service.log. This log file will also contain details of the error if the application fails to start.

Step 3: Securing the service

Any service executable will start a process with the same user as the owner of that executable. With that in mind,

  • Do not use root account to run spring boot application or any other application service for the sake of security. It is considered a bad practice. to handle this,
  • Create a separate user called springrunner using the command adduser springrunner --shell=/bin/false. This command will create a system user with no login. If interested, google a little about /bin/false.
  • Change the owner of the executable jar using chown springrunner:springrunner /var/applications/hello-world.jar
  • In order to make the jarfile not accessible by others, chmod 500 /var/applications/hello-world.jar

Step 4: Custom configuration file for the service

By default this spring boot service will look for a hello-world.conf file next to the hello-world.jar with same name. The .conf file should have same name as the jar name. This file is supposed to contain some of the basic service parameters like PID_FOLDER, LOG_FOLDER, RUN_ARGS, JAVA_OPTS, DEBUG, STOP_WAIT_TIME and much more. A sample config file is shown below.

RUN_ARGS=arg1,arg2
JAVA_OPTS=-Xmx2048M
LOG_FOLDER=/path/to/log/file.txt
STOP_WAIT_TIME=10

But this .conf file is completely optional.

Raja Anbazhagan

About the author

Raja is a Software Engineer with over 7 years of experience in working with Enterprise Java applications. Lately, He is focused in cloud-based Java applications and serverless technologies. He spends his spare time in stackoverflow.

Browse Categories