How to create a systemd service?

Introduction

In this guide we will create a systemd service and make it automatically start on a boot.

We have already seen how we can make commands execute on a boot using cron scheduler in this post. Creating systemd service file is an alternative way of doing this, and since systemd is becoming more and more popular among Linux distributions, learning it is worth the time. I’m using Ubuntu 18.04 for this guide, but the following steps apply to any distribution that uses systemd.

Create a program

First let’s create a program, that will be managed by our service. We will create a simple shell script that will print out a current time, then we will create a systemd service that will manage our program and run it on startup.

Create a file with the following content and name it timelogger.sh:

#!/bin/bash
while true
do
  echo The time is $(date +%X)
  sleep 2
done

 

This is a shell script file which will keep printing a time every 2 seconds. Before you can run it make it executable by running this command chmod +x timelogger.sh

Now you can run the shell script and see the output:

orkhans@matrix:~$ ./timelogger.sh 
The time is 09:33:49 PM
The time is 09:33:51 PM
The time is 09:33:53 PM
^C

 

The program will run infinitely, therefore you should press CTRL+C to exit.

Create a systemd service

Now that we have a program we will go ahead and create a service file, that will manage our timelogger.sh

Navigate to /etc/systemd/system directory and create a timelogger.service file, using any text editor, for example sudo nano timelogger.service.

Here’s the content of my file:

orkhans@matrix:/etc/systemd/system$ cat timelogger.service 
[Service]
ExecStart=/home/orkhans/timelogger.sh

 

You will need root privileges to create this file, because ordinary users do not have write permission for /etc/systemd/system  directory.

After you have created the service file you can start the newly created service by running sudo systemctl start timelogger:

orkhans@matrix:/etc/systemd/system$ sudo systemctl start timelogger

 

Next, run the sudo systemctl status timelogger command to see the status of your service:

orkhans@matrix:/etc/systemd/system$ sudo systemctl status timelogger
● timelogger.service
   Loaded: loaded (/etc/systemd/system/timelogger.service; static; vendor preset
   Active: active (running) since Mon 2018-06-25 21:47:54 +04; 2min 6s ago
 Main PID: 13706 (timelogger.sh)
    Tasks: 2 (limit: 4588)
   CGroup: /system.slice/timelogger.service
           ├─13706 /bin/bash /home/orkhans/timelogger.sh
           └─13878 sleep 2

Jun 25 21:49:43 matrix timelogger.sh[13706]: The time is 09:49:43 PM
Jun 25 21:49:45 matrix timelogger.sh[13706]: The time is 09:49:45 PM
Jun 25 21:49:47 matrix timelogger.sh[13706]: The time is 09:49:47 PM
Jun 25 21:49:49 matrix timelogger.sh[13706]: The time is 09:49:49 PM
Jun 25 21:49:51 matrix timelogger.sh[13706]: The time is 09:49:51 PM
Jun 25 21:49:53 matrix timelogger.sh[13706]: The time is 09:49:53 PM
Jun 25 21:49:55 matrix timelogger.sh[13706]: The time is 09:49:55 PM
Jun 25 21:49:57 matrix timelogger.sh[13706]: The time is 09:49:57 PM
Jun 25 21:49:59 matrix timelogger.sh[13706]: The time is 09:49:59 PM
Jun 25 21:50:01 matrix timelogger.sh[13706]: The time is 09:50:01 PM
lines 1-19/19 (END)

You can see that the status of the service is active (running)  and you can also see the produced output.

You can stop the service by running sudo systemctl stop timelogger:

orkhans@matrix:/etc/systemd/system$ sudo systemctl stop timelogger
orkhans@matrix:/etc/systemd/system$ sudo systemctl status timelogger
● timelogger.service
   Loaded: loaded (/etc/systemd/system/timelogger.service; static; vendor preset
   Active: inactive (dead)

Jun 25 21:53:01 matrix timelogger.sh[13706]: The time is 09:53:01 PM
Jun 25 21:53:03 matrix timelogger.sh[13706]: The time is 09:53:03 PM
Jun 25 21:53:05 matrix timelogger.sh[13706]: The time is 09:53:05 PM
Jun 25 21:53:07 matrix timelogger.sh[13706]: The time is 09:53:07 PM
Jun 25 21:53:09 matrix timelogger.sh[13706]: The time is 09:53:09 PM
Jun 25 21:53:11 matrix timelogger.sh[13706]: The time is 09:53:11 PM
Jun 25 21:53:13 matrix timelogger.sh[13706]: The time is 09:53:13 PM
Jun 25 21:53:15 matrix timelogger.sh[13706]: The time is 09:53:15 PM
Jun 25 21:53:17 matrix systemd[1]: Stopping timelogger.service...
Jun 25 21:53:17 matrix systemd[1]: Stopped timelogger.service.
lines 1-14/14 (END)

Now the status says inactive(dead)

 

Make the service start on a boot

Now that we have service that controls our shell script (program), let’s make our service start on a boot.

Navigate to /etc/systemd/system directory and make some additions to our service file timelogger.service. Make sure it looks like this:

orkhans@matrix:/etc/systemd/system$ cat timelogger.service 
[Service]
ExecStart=/home/orkhans/timelogger.sh

[Install]
WantedBy=multi-user.target

This line WantedBy=multi-user.target makes our service start on startup. Besides that, we need to enable our service:

orkhans@matrix:/etc/systemd/system$ sudo systemctl enable timelogger
Created symlink /etc/systemd/system/multi-user.target.wants/timelogger.service → /etc/systemd/system/timelogger.service

 

Then we need to reload the systemd, because we made some changes to the service file:

orkhans@matrix:/etc/systemd/system$ sudo systemctl daemon-reload

 

Now we can reboot the computer and we will see after reboot, that our service started automatically:

orkhans@matrix:~$ sudo systemctl status timelogger.service 
[sudo] password for orkhans: 
● timelogger.service
   Loaded: loaded (/etc/systemd/system/timelogger.service; enabled; vendor prese
   Active: active (running) since Mon 2018-06-25 22:07:11 +04; 39s ago
 Main PID: 711 (timelogger.sh)
    Tasks: 2 (limit: 4588)
   CGroup: /system.slice/timelogger.service
           ├─ 711 /bin/bash /home/orkhans/timelogger.sh
           └─1686 sleep 2

Jun 25 22:07:32 matrix timelogger.sh[711]: The time is 10:07:32 PM
Jun 25 22:07:34 matrix timelogger.sh[711]: The time is 10:07:34 PM
Jun 25 22:07:36 matrix timelogger.sh[711]: The time is 10:07:36 PM
Jun 25 22:07:38 matrix timelogger.sh[711]: The time is 10:07:38 PM
Jun 25 22:07:40 matrix timelogger.sh[711]: The time is 10:07:40 PM
Jun 25 22:07:42 matrix timelogger.sh[711]: The time is 10:07:42 PM
Jun 25 22:07:44 matrix timelogger.sh[711]: The time is 10:07:44 PM
Jun 25 22:07:46 matrix timelogger.sh[711]: The time is 10:07:46 PM
Jun 25 22:07:48 matrix timelogger.sh[711]: The time is 10:07:48 PM
Jun 25 22:07:50 matrix timelogger.sh[711]: The time is 10:07:50 PM

Thank you for reading!

 

Add a Comment