Systemd

Service File Templates

To create several similar services, service file templates can be used in the following way.

First, create the template, where is a meaningful general name for the service type: Note that %i gets expanded to the service name

# /etc/systemd/system/<NAME>@.service
[Unit]
Description=Describe service %i

[Service]
## Type: one of simple, forking, oneshot, dbus, notify or idle
# Type=simple
## useful in conjunction with oneshot
# RemainAfterExit=yes
## Optionally set environment variables in the template, we'll override them later
# Environment=SERVICE_SPECIFIC_ENV_VAR=default_value
ExecStart=/path/to/cmd $SERVICE_SPECIFIC_ENV_VAR
# ExecReload=whatever
# ExecStop=whatever

[Install]
WantedBy=multi-user.target

Then, enable individual services by

systemctl enable <NAME>@<INSTANCE_NAME>.service

Now, to define service specific behavior:

# /etc/systemd/system/<NAME>@<INSTANCE_NAME>.service.d/overrides.conf
[Service]
Environment=SERVICE_SPECIFIC_ENV_VAR=override_value

Depending on your version of systemd, you might be able to create this override file by just using systemctl edit

systemctl edit <NAME>@<INSTANCE_NAME>.service

Otherwise, you can also create it manually. To view the resulting service file, you can do

systemctl cat <NAME>@<INSTANCE_NAME>.service

Useful resource: Systemd doc from freedesktop.org

Prevent long network interface names

Some network adapters' firmware (I had an issue e.g. with realtek rtl_nic/rtl8168e-3.fw) does not handle long network interface names properly. You can make systemd use the good old wlan0 style using the following command:

ln -s /dev/null /etc/systemd/network/99-default.link