[systemd-devel] Dynamically restart a service via dbus

Matwey V. Kornilov matwey.kornilov at gmail.com
Sat Feb 1 11:38:20 UTC 2025


Hi,

Let me please describe my initial problem and then explain what did I 
try and what is my issue.

I have a systemd service with Restart=always and pretty large 
RestartSec, say 10 minutes. The service executable communicates with a 
remote host, when the host becomes unavailable the service exits. If 
systemd tries to restart the service when the host is unavailable then 
the service exits shortly after the process is spawned. The current 
configuration performs unattended service recovery in face of the host 
unavailability.

I want to improve this configuration. It appeared that I can track an 
event when the host is up again. More precisely, I use dbus events of 
dnsmasq DHCP server. Initially, I decided just to call RestartUnit() at 
org.freedesktop.systemd1.Manager dbus interface. But I quickly realized 
that this solution doesn't respect whether the service is enabled or 
disabled by administrator. I could get the service enabled/disabled 
state via dbus before, but it will not be atomic. There is also race 
condition when the service is being restarted and I issue RestartUnit() 
call, but it seems to be handled by mode parameter.

My question is whether there is better solution for my initial problem 
or is there a way to atomically handle units via dbus systemd interface?



More information about the systemd-devel mailing list