How to create a Systemd service unit file in Linux

A service is a program that runs automatically when the computer starts and waits in the background to do its job. A service usually does not have a graphical user interface and works without user interaction. The best-known services are certain web, mail, or database servers, for example, apache, MySQL, and many others. But also the hardware detection or the automatic integration (mounting) of USB sticks, for example, is done by services.

In principle, there are two types of services: internal, for tasks that are relevant or hardware-related when the system is started, and other is services that are subsequently installed by the user, which usually include all server services. In technical terms or computer jargon, services are also traditionally referred to as daemons. The letter ‘d’ therefore often used as the last letter in the program to denote some services, such as when the server component sshd of SSH or mysqld of MySQL.

Whereas Systemd is a system and session manager (init system) that is responsible for managing all services running on the system over the entire operating time of the computer, from the start-up process to shutdown. Processes are always started in parallel (as far as possible) to keep the boot process as short as possible. Now, when we create a configuration file that ends with .service and holds code about a process controlled and supervised by Systemd; is know as a Systemd Service Unit file. Units are created for services, timers, mount points, sockets, swap space, and devices, for example.

Hence, systemd gets all its defaults and settings for administration from files, in systemd terminology, these are “units”. A distinction is made between units that apply throughout the system and those that only apply to the respective user area.

There are various types of units such as service units for starting services or timer units for (repeated) execution of an action at a certain point in time. All types of unit files have in common that they have a structure similar to ini files. They consist of several sections (in most cases three), referred to in systemd known as “sections”, in which a series of key-value pairs, in systemd called “directives”, are stored.

Command Line

The tool for managing systemd on the command line or in a terminal is called systemctl. Most commands intervene deeply in the system and therefore require root rights, hence this command must be run under sudo rights to perform the required operations.

Logs

systemd writes log information to a central journal. This can be read out with the help of journalctl, including search and filter options.  

system-wide units

usr/lib/systemd/system: This is where all files of units that have been preinstalled system-wide by services are located. /etc/systemd/system: All files of system-wide units that are created or edited by users are located here. Root rights are required for this. If there are unit files under /etc/systemd/system with the same name as under usr/lib/systemd/system, preference is given to those from /etc systemd/system, i.e they are loaded by the system.  

Types of units

There are different types of units, which are handled differently by systemd depending on the file name extension:

Type description  
.device Creates device files  
.mount Mounting and unmounting of file systems  
.path Execute a service unit depending on a change to a file or directory  
.network For the configuration of networks  
.service For services  
.socket Establishes connections between processes  
.target Defines a group of units  
.timer For recurring tasks, similar to cron jobs  

Here we talk about the .Service Unit  

Create your own Simple unit or systemd service file

Let’s say, you want to run some script or software in the background with the system boot. Thus, for that, we need to create a service unit. The syntax for that is given here:

sudo nano /usr/lib/systemd/system/file-name.service

or

sudo nano /etc/systemd/system/file-name.service

Replace- file-name  with the name you want to give Paste the below-given block of text:

[Unit]
Description = what service is for
After = network.target

[Service]
ExecStart = path to files/script that must be executed

[Install]
WantedBy = multi-user.target

Change the bold items in the above code block and Save the file- press Ctrl+X, type- Y, and hit the Enter key. For example:  If we want to create a service file for Glances Linux monitoring tool to make it run in the background, the above steps will be like this:

sudo nano /usr/lib/systemd/system/glances.service

Paste the below-given block of text:

[Unit]
Description = Glances in Web Server Mode
After = network.target

[Service]
ExecStart = /usr/bin/glances -w -t 5

[Install]
WantedBy = multi-user.target

Save the file- press Ctrl+X, type Y, and then hit the Enter key.

Explanation:

[Install]:

In the “[Install]” section, the key WantedByspecifies when the unit is started. Different values ​​are possible:

Target description
multi-user.target for multi-user systems, with or without graphical login (corresponds to runlevel 3)
graphical.target for multi-user systems that must have a graphical login interface (corresponds to runlevel 3 plus graphical login)
rescue.target Single user mode is usually only required for system rescue (corresponds to runlevel 1)
reboot.target Unit is only executed when the system is restarted
poweroff.target The unit only runs when the system starts shutting down
default.target This default.targetis not a “real” target, but a symbolic link to another, real existing target. In the standard installation of the Ubuntu desktop, this default.targetis graphical.target.

 

Activate the manually create systems service units

Once you have created the service file using the above-given steps, then we also need to enable and start it. For that commands are: Reload the service files to include the new service.

sudo systemctl daemon-reload

To Enable and start the service file:

sudo systemctl enable filename.service
sudo systemctl start filename.service
sudo systemctl status filemane.service

Replace the filename with the one you have given to your service file. For example, if we go for glances service created above then, to enable it the command will be- sudo systemctl enable glances.service    

 

 

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.