Introduction

How to create a systemd service in 2 easy steps.

systemd service
Reading Time: 3 minutes

As we all know, it will always be needed to run an application as a service because sometimes we want our application to run in the background as a service daemon. So, in that case, we create Systemd service files or unit files to run the application.

Introduction to systemd and service

systemd is an init system and system manager that has widely become the new standard for Linux distributions. Due to its heavy adoption, familiarizing yourself with systemd is well worth the trouble, as it will make administering servers considerably easier. Learning about and utilizing the tools and daemons that comprise systemd will help you better appreciate the power, flexibility, and capabilities it provides, or at least help you to do your job with minimal hassle.

We will be discussing the systemctl command, which is the central management tool for controlling the init system. We will cover how to manage services, check statuses, change system states, and work with the configuration files.

First, let’s start out by discussing the difference between a service and a process. In Linux, a service is just a daemon, which is a client/server application that runs in the background.

A service continuously listens for incoming requests and sends a response based on the request given but the process is simply an application or a script that runs in the foreground or the background.

Basic Synopsis for creating a systemd service file

[Unit]
Description=<General description of your application>

[Service]
type=simple
ExecStart=<path_to_script>/<app_script.sh>  #To start the application 
ExecStop=/bin/<path_to_script>/<app_stop_script.sh> #To stop the application

[Install]
WantedBy=multi-user.target

This is a basic structure of a service file.

Save this file with .service extension.


Demo:

Now, let’s create a service file to run nginx as a service

my-ngnix.service

[Unit]
Description=mynginx- server start

[Service]
type=forking
PIDFile=/run/nginx.pid
WorkingDirectory=/usr/sbin/
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

Save this file in /etc/systemd/system

[Unit] carries generic information about units that are not dependent on the type of unit.

OptionDescription
DescriptionA human-readable name for the unit.

[Service] which carries information about the service and the process it supervises

OptionDescription
type=forkingThe parent process is expected to exit. The child continues to run as the main service process.
PIDFile=/run/nginx.pidTakes a path referring to the PID file of the service.
WorkingDirectory=/usr/sbin/Current working directory relative to the app.
ExecStart=/usr/sbin/nginxCommand or script with arguements.
ExecReload=/usr/sbin/nginx -s reloadCommands to execute to trigger a configuration reload in the service.
ExecStop=/bin/kill -s QUIT $MAINPIDCommands to execute to stop the service started via ExecStart
PrivateTmp=trueIf true, all temporary files created by a service in /tmp/ and /var/tmp/ will be removed after the service is stopped.

[Install] section carries installation information for the unit.

OptionDescription
WantedBy=multi-user.targetIt specifies how a unit should be enabled. multi-user.target.wants will be created within /etc/systemd/system and a symbolic link to the current unit will be placed within.

To run this service

sudo systemctl start my-nginx.service

To see Status of Service

sudo systemctl status my-nginx.service

The output will look like this:

● my-nginx-service.service - mynginx- server start
Loaded: loaded (/etc/systemd/system/my-nginx-service.service; disabled; vendor preset: enabled)
Active: active (running) since Tue 2021-09-28 12:17:21 IST; 7h ago
Main PID: 1166 (nginx)
Tasks: 0 (limit: 18772)
Memory: 8.0K
CGroup: /system.slice/my-nginx-service.service
‣ 1166 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;


References

  • Manual page of unit and service files

Thanks for reading this blog. Check out our website knoldus.com for engineering solutions and you can find more awesome blogs at https://blog.knoldus.com

Written by 

Rahul Soni is a Software Consultant at Knoldus Software. He is always charged up for new things & learnings. He is dedicated to his work and believes in quality output. He loves to take deep dives into cloud technologies & different tools.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading