Introduction

To make an application of several big or small tasks a Service is used on the system. In this example we are creating a service which will help us to deploy a SpringBoot JAR application as a service on Ubuntu Linux machine.

Prerequisites

  • Ubuntu 20.04 installed
  • User account with sudo privileges
  • Access to Terminal / Command Line
  • Java JRE installed

The following steps are reqired to deploy a JAR as a service on Ubuntu Linux.

Step-1

Create a file as per the required name with .service extension at /etc/systemd/system/ path. In this example we are using SpringBootJARApplication.service

sudo nano /etc/systemd/system/SpringBootJARApplication.service

The sample file is as follows:

[Unit]
Description=SpringBoot JAR Service
Requires=network.target
After=network.target syslog.target

[Service]
Type=simple
User=change-user-name
Group=change-user-group-name
ExecStart=/usr/bin/java -jar /opt/deploy/SpringBootApp.jar
Restart=on-failure
ReatartSec=10s
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=SpringBootApp

[Install]
WantedBy=default.target

Step-2

After successfully creating the file and saving the content in it, now start the service using following command

sudo systemctl start SpringBootJARApplication.service

Step-3

As the sudo systemctl start SpringBootJARApplication does not produces any output, the following command can be used to check the status of started service

sudo systemctl status SpringBootJARApplication.service

Stop or Restart the service

To STOP the service use the following command

sudo systemctl stop SpringBootJARApplication.service

To RESTART the service use the following command

sudo systemctl restart SpringBootJARApplication.service

Step-4

Enable the service at system startup

sudo systemctl enable SpringBootJARApplication.service

Step-5

Once the SpringBootJARApplication.service is created then reload the services

systemctl daemon-reload