How to create a systemd service unit file in Linux

systemd is a system and service manager for Linux operating systems. When you install any application from a repository, it will drop a service unit file into the systemd directory and you should not modify these files directly.

The systemd unit files will be found in the following three directories:

  • /usr/lib/systemd/system/ – systemd unit files dropped when the package has installed.
  • /run/systemd/system/ – systemd unit files created at run time.
  • /etc/systemd/system/ – systemd unit files created by ‘systemctl enable’ command as well as unit files added for extending a service.

Sometime you may need to create a service unit file for a custom application or daemon or script. There are many parameters can be added, but we will only add a few values to make the unit file much simpler for better understanding:

Refer the following articles to know more about systemd & systemctl:

For instance: To run a custom script on boot in systemd system, you need to create a custom service unit file. The following procedure describes the general process of creating a custom service unit:

Creating a custom script

The following shell script will write the welcome message in the file as follows:

$ sudo vi /usr/sbin/welcome.sh

#!/bin/bash
echo "Welcome to Linux WORLD..!!!" >> /tmp/welcome.txt

Creating systemd unit file

You will need to create a custom service unit file under the ‘/etc/systemd/system/’ directory because this is reserved for custom scripts. Any unit file in ‘/etc/systemd/system’ will override the corresponding file in ‘/lib/systemd/system’.

Syntax: The systemd unit file consists of three sections:

Section-1 [Unit]
Parameter 1
.
.
Parameter N

Section-2 [Service]
Parameter 1
.
.
Parameter N

Section-3 [Install]
Parameter 1

To demonstrate this, we will be creating a systemd service unit file named ‘custom.service’.

$ sudo vi /etc/systemd/system/custom.service

[Unit]
Description=example systemd custom service unit file
After=network.target

[Service]
Type=notify
ExecStart=/bin/bash /usr/sbin/welcome.sh

[Install]
WantedBy=multi-user.target

Section-1:

  • Unit : This section provides basic information about the service
  • Description : Short description of the service unit. The description appears next to the service unit name when you execute ‘systemctl status UNIT.service’ command.
  • After : Defines the order in which units are started. The ‘custom.service’ unit will be started only after the ‘network.target’ unit is started.

Section-2:

  • Service : The ‘Service’ section provides instructions on how to control the service.
  • Type : Defines the type of systemd service. It’s identical to ‘Type=simple’, but at the same time the daemon expected to send a signal to systemd when it is ready.
  • ExecStart : It used to start the service, which includes the full path to the actual service executable.

Section-3:

  • Install : The ‘Install’ section provides instructions on how to install the systemd service.
  • WantedBy : The ‘WantedBy’ setting indicates under which target a given service unit should be launched. In this example, custom.service uses multi-user.target, so systemd starts custom.service when loading multi-user.target on startup.

Set the executable permission to the ‘custom.service’

$ sudo chmod a+x /etc/systemd/system/custom.service

To add a new service to systemd, run:

$ sudo systemctl daemon-reload

To start the custom.service, run:

$ sudo systemctl start custom.service

To enable the custom.service on boot, run:

$ sudo systemctl enable custom.service

Finally reboot the system to check if the custom.service ran the script on boot as expected by verifying the output file.

$ sudo reboot

Yes, it worked well.

$ cat /tmp/welcome.txt
Welcome to Linux WORLD..!!!

Ref: systemd the unit file structure

Over to You

In this guide, we have explained how to create a custom systemd service unit file in Linux.

If you have any questions or feedback, feel free to comment below.

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

One Comment on “How to create a systemd service unit file in Linux”

Leave a Reply

Your email address will not be published. Required fields are marked *