Site icon Super Computer World

How to run a Spring Boot Application as a Linux Service?

How to run a Spring Boot Application as a Linux Service_

This post will show how to run a Spring Boot application as a Linux service configured from bash with an external configuration.

What you are going to see in this post

Packaging, Configuration and Construction of the Project

First, we will explain the packaging options, the structure of the project, and its structure.

Spring Boot Application Packaging

Spring Boot applications can be called WAR and JAR files.

Configuration of our pom.xml

The first thing we must do is the configuration of our pom.xml, to which we have to add the parent dependency (org.springframework.boot:spring-boot-starter-parent:2.4.3) and the configuration of the build:

<packaging> jar </packaging>

<parent>

<groupId>org. springframework. boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.4. 3</version>

</parent>

<dependencies>

….

</dependencies>

<build>

<plugins>

<plugin>

<groupId>org. springframework. boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

<configuration>

<executable>true</executable>

</configuration>

</plugin>

</plugins>

</build>

Note: The packaging jar comes by default, so you do not need to put it.

Set the parameter to true for the spring-boot-maven-plugin artefact. This causes the MANIFEST file to be added. MF to the JAR package. This manifest contains a parent class entry that specifies which class defines the primary method for your c.

Building our App

The next step will be the execution of the Maven clean package command. The following command inside the root directory of your application:

mvn clean package

The executable jar file is available in the Target folder.

We can run our jar simply with the command:

java -jar mi-profile-app. jar

Configuring a Service on Linux

The following sections explore different alternatives we have when setting up a service on Linux.

Getting a Spring Boot executable with Jar packaging to service is straightforward.

The first thing we must do is place the jar on the desired route. In our case, we will place it on the following route:

/var/applications/my profile app/my-profile-app. Jar

We will only have to create a link between our files and assign it to the service. For doing this, we must run the following line in the console:

ln -s /var/applications/miProfileApp/mi-profile-app. jar /etc/init. d/profile_app_service

Then the only thing we would have to do is the initialization of the service by executing the following line:

service profile_app_service start

The operation of these last two commands will be explained later.

What happens is that this option would work with the configuration file that exists within our jar file. To execute an external configuration file to the one that exists in the jar, we must create a bash that is the one that we associate with our service.

Therefore, we will proceed to explain where we should place our configuration file, the structure of our bash, the permissions it must have, how to create the reference to the service and the start/stop processes and how to see the active assistance of our server.

Folder Structuring

The first thing we have to be clear about is where we will place our configuration files, our jar, etc…

For this we have decided on the following structure:

/var

/ – applications

/ – myProfileApp

| + my-profile-app-1.0. 0-SNAPSHOT. Jar

/ – config

| + application. yml

/ – scripts

| + mi_profile_app. sh

Application.yml

Firstly we should do place our file in the corresponding path:

/var/applications/my profile app/config/application. yml

Creating the .sh

Once we have placed the configuration file in the corresponding path, we will create the bash file, which will contain the information to execute the external configuration.

To do this, we will create our bash “mi_profile_app.sh” in our script path. To create the she, we will do the following:

vi /vars/scripts/mi_profile_app. sh

And as the content of our bash would be:

#!/bin/bash

JAR_NAME = my-profile-app-1.0. 0-SNAPSHOT. Jar

OPTIONS = “-c /var/applications/myProfileApp/config/application.yml”

cd /var/applications/my profile app

“${JAVA_HOME}/bin/java” ${JAVA_OPTIONS} -jar $JAR_NAME $OPTIONS

We understand that we will have configured the environment variables of JAVA_HOME and JAVA_OPTIONS. In case of not have configured the JAVA_OPTIONS, we can add to our bash the following line:

JAVA_OPTIONS=” -Xms256m -Xmx512m -server ”

Permissions from our Bash

When we have created our bash file, we will give it permissions using the user (u) and execution (x) chmod command. The line that we will put on our console would be the following:

chmod u+x mi_profile_app. sh

Service Reference

To create our bash as a reference for our service, we must write the following line:

ln -s /var/scripts/mi_profile_app. sh /etc/init. d/profile_app_service

This command does the creation of a symbolic link to our bash file. The path where it is in the script must be the full path. Otherwise, the symbolic link does not work correctly.

Starting the Service

Once our reference is created, we can launch our Spring Boot application as a Linux service. For doing this, we will perform the following command:

service profile_app_service start

Service Stop

If Incase, we want to stop our service, we will execute the stop command:

service profile_app_service stop

View Active Services

If what we want is to see all the active services we have, we can execute the command:

service –status-all

Conclusion

We can efficiently manage our Spring Boot application’s state through a Linux service and have all its configuration outsourced, which will always be ready to start or stop according to our needs. After all, the important thing is to have the design of our application in an external file, which we can change the content of it, and we would only have to restart our service.

Exit mobile version