How to create a system service in Linux

How to create a system service in Linux

Run your app as a service

You have this awesome app on your machine and want to run it as a service. You want to automate the whole process so you don't have to go through using the language framework start up commands each time. Say you want to start the app in the background of your machine where you can start, restart, check status and stop at will. Hold your beer!

Systemd and Systemctl

systemd is a system and service manager for Linux operating systems. This manager allows you to create and manage services in extremely powerful and flexible ways.

All services managed by systemd can be located at:

sudo cd /etc/systemd/system/

systemctl may be used to introspect and control the state of the systemd system and service manager. systemctl must be run with sudo

sudo systemctl start service.service
sudo systemctl stop service.service
sudo systemctl restart service.service
sudo systemctl status service.service

Creating a service

We create our service:

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

Our service configuration

[Unit]
Description=My custom service
Requires=network.target

[Service]
WorkingDirectory=/home/user/workspace
ExecStart=/home/user/workspace/app
SuccessExitStatus=143
TimeoutStopSec=10
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target

The [Unit] section provides basic information about the service. Description describes the service. Systemd displays this description next to the unit name in the user interface. Requires defines unit to use as a dependency for the service. Here we say our app depends on the network service.

The [Service] section provides instructions on how to control the service. WorkingDirectory specifies the directory the service will be working in. ExecStart defines the command to run the service. This includes the full path to the command and arguments to modify the service.SuccessExitStatus defines status that when returned by the main service process, will be considered successful termination. TimeoutStopSec configures the time to wait for each ExecStop command. Restart configures whether the service shall be restarted when the service process exits, is killed, or a timeout is reached. RestartSec configures the time to sleep before restarting a service.

The [Install] section provides instructions on how systemd installs the service. WantedBy defines which service triggers the custom service if enabled with systemctl enable. This is mostly used for starting the custom service on boot. In this example, myapp.service uses multi-user.target, which starts myapp.service when systemd loads multi-user.target on boot.

Creating our app

We create our bash script

sudo nano /home/user/workspace/app

This bash script executes out application(.jar) using a java executable.

#!/bin/sh
sudo /usr/bin/java -jar payroll-0.01-SNAPSHOT.jar

We need to give our app file executable permission.

sudo chmod u+x app

Start Service

sudo systemctl daemon-reload
sudo systemctl enable  myapp.service
sudo systemctl start myapp
sudo systemctl status myapp

Logging

Now your service is running in the background. You might be wondering how to check your application logs, well linux got you.

View the last 100 logs of your service

sudo journalctl -f -n 100 -u myapp

Tailing your service

sudo journalctl -f  -u myapp

Stop Service

sudo systemctl stop myapp

I do hope you enjoyed this article, show your support if you like it. Thanks!