Systemd – Create Your Own Unit Files
In most enterprises you will meet with systemd.
It’s a Linux system and service manager that handles the management of daemons and processes.
Most packages installed, for example, by apt will ship systemd unit files too.
But some of them need adjustments.
For your own tools and services you have to write your own unit files.
Systemd Unit Files
The manual page systemd.unit contains extensive information about the systemd unit files.
A unit file is a plain text ini-style file that encodes information about a service, a socket, a device, a mount point, an automount point, a swap file or partition, a start-up target, a watched file system path, a timer controlled and supervised by, a resource management slice or a group of externally created processes.
The unit files managed by the administrator are under /etc/systemd/system.
To write such a file, I use the file I prepared for my Gitea server.
Example Gitea Unit File
[Unit]Description=GiteaAfter=network.target[Service]RestartSec=2sType=simpleUser=gitGroup=gitWorkingDirectory=/var/lib/gitea/ExecStart=/usr/local/bin/gitea web --config /etc/gitea/app.iniRestart=alwaysEnvironment=USER=git HOME=/home/git GITEA_WORK_DIR=/var/lib/gitea[Install]WantedBy=multi-user.target
Explanation
The unit file is divided into three sections: [Unit], [Service] and [Install].
The [Unit] section contains general information about the unit.
Description gives the unit a human-readable name.
After defines the start-up order. In this example, Gitea will be started after network.target.
The [Service] section contains the configuration of the service itself.
User and Group define the user and group under which Gitea will run.
WorkingDirectory defines the working directory of the process.
ExecStart contains the command that systemd will execute to start Gitea.
Restart tells systemd to restart Gitea if it exits. RestartSec defines the delay before the restart.
Environment sets environment variables for the Gitea process.
The [Install] section defines how the unit can be enabled.
WantedBy specifies the target where the unit should be enabled. In this case, enabling the unit will make Gitea part of the multi-user.target.
After creating or changing a unit file, systemd needs to reload its configuration.
systemctl daemon-reload
The service can then be started:
systemctl start gitea
To start Gitea automatically when the system boots, enable the unit:
systemctl enable gitea
The status of the service can be checked with:
systemctl status gitea
And if the service fails, its logs can be inspected with:
journalctl -u gitea
Final Thoughts
Creating your own systemd unit files is not complicated.
The important part is understanding what you want systemd to manage and how that process should behave.
Once you understand the basic structure of a unit file, you can create services for your own applications and tools instead of starting them manually.
For more complex services, the systemd.service and systemd.unit manual pages contain everything you need to go further.
Discover more from Tom's IT Cafe
Subscribe to get the latest posts sent to your email.