July 14, 2020 | 07:04

Systemd Drop In

At work, I was debugging an issue where it was failing to update the systemd configuration to the new installation path.

By default, systemd service definitions go under /etc/systemd/system/name.service. There are cases where you as an administrator have no control over the service generated. So how do you override a service configuration without overriding the original service or the service definition from scratch?

Systemd has a functionality called drop-in units where you can write parts of the service definition and it will override it.

For example in the GitLab Runner scenario we had:

# cat /etc/systemd/system/gitlab-runner.service
[Unit]
Description=GitLab Runner
After=syslog.target network.target
ConditionFileIsExecutable=/usr/bin/gitlab-runner

[Service]
StartLimitInterval=5
StartLimitBurst=10
ExecStart=/usr/bin/gitlab-runner "run" "--working-directory" "/home/gitlab-runner" "--config" "/etc/gitlab-runner/config.toml" "--service" "gitlab-runner" "--syslog" "--user" "gitlab-runner"

Restart=always
RestartSec=120

[Install]
WantedBy=multi-user.target
# cat /etc/systemd/system/gitlab-runner.service.d/exec_start.conf
[Service]
ExecStart=/usr/lib/gitlab-runner/gitlab-runner "--log-format" "json" "run" "--working-directory" "/home/gitlab-runner" "--config" "/etc/gitlab-runner/config.toml" "--service" "gitlab-runner" "--user" "gitlab-runner"

In this case /etc/systemd/system/gitlab-runner.service.d/exec_start.conf is overrideing the [Service] section.

If you want to see if the service you are debugging has any drop-in units you can run systemctl show name.service and it will show all the configuration loaded.