🏠Spring BootInstall and Run Spring Boot Linux Service Guide

Install and Run Spring Boot Linux Service Guide

Let’s learn how to install a spring boot as a Linux service.

Requirements

You will need,

Make the jar an executable

Spring boot applications can be started using the command java -jar hello-world.jar. We have seen this behavior 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 add the following configuration entry.

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <executable>true</executable>
    </configuration>
</plugin>Code language: HTML, XML (xml)

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.

Configure spring boot linux service

Once you have the executable jar file, You can symlink it to the /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-serviceCode language: JavaScript (javascript)

On some Linux varients, you need to enable the service using the following command.

sudo systemctl daemon-reload

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

sudo service hello-service start

With the above setup,

  • You can startstoprestart and check status of the Spring Boot 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 doesn’t stop within 60 seconds, it will be force killed. And this timeout is configurable.
  • The running process will have the same ownership as the file. That is, If the file is owned by springadmin, then the process will also be created with springadmin.
  • 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.

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 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

Customize the Spring Boot service

By default, this spring boot service will look for a hello-world.conf file next to the hello-world.jar with the same name. The .conf file should have the same name as the jar name. This file is supposed to contain some of the basic service parameters like PID_FOLDERLOG_FOLDERRUN_ARGSJAVA_OPTSDEBUGSTOP_WAIT_TIME and much more. The following is a sample configuration.

RUN_ARGS=arg1,arg2
JAVA_OPTS=-Xmx2048M
LOG_FOLDER=/path/to/log/file.txt
STOP_WAIT_TIME=10Code language: JavaScript (javascript)

But this .conf file is completely optional.

You can find a sample project for this at this GitHub repository.

Related

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

One Comment