Caution! Article for developers

Information on this page is intended particularly for users with advanced technical knowledge.

Creating a custom systemd service

Dec 14, 2022 · 2 minutes to read

Treon Gateway

The gateway uses systemd as it’s init/service manager. This article helps you add your own service or unit file to start your application automatically after boot process is finished and make it persistent between updates. This article will not teach you how to create your systemd unit file.

Because the gateways update process wipes the system partition mounted to / after each update, the unit file has to be persisted somehow. Unfortunately we cannot use the persist_move utility described in here to persist the unit file because persist_move is executed after systemd has read the unit files. However we can use the updatehooks to copy the unit file to system partition when the gateway is running it’s update process.

Tips for unit files and verifying your unit file works

First you will need to create your unit file. The unit file should be placed to e.g. /lib/systemd/system-folder and after the content has been changed, then sudo systemctl daemon-reload should be run.

We have used:

[Service]
ExecStartPre=/usr/bin/test -f /etc/first_boot_done

[Install]
WantedBy=multi-user.target

To make sure the service is not run when gateway is updating and to make sure it does not start too early.

After creating this unit file and running the sudo systemctl daemon-reload-command the service should start automatically and can be verified to run with systemctl status <service-name>.

Persisting the unit file

To make sure the unit file is persisted over gateway firmware updates it has to be installed each time the gateway has updated to new firmware (due to the firmware update wiping system partition). This can be done with updatehooks by creating updatehook which copies the unit file to it’s system partition location during update process.

Example updatehook:

#!/bin/bash

cp /mnt/data/my_application.service /lib/systemd/system/
systemctl daemon-reload
systemctl enable my_application

For this to work the unit file has to be located int /mnt/data/my_application.service.


Was this article helpful? Let us know.
Next