systemd: parent service that fails if child services fail
I've been working on a more robust kea-dhcp4 server implementation, to work around some of the application's inherent weaknesses. Two specific things I have to deal with, (1) it doesn't have a syntax check command, so you have to rely on the startup failure to determine if you made a typo (2) if the database connection is lost, the daemon just exits.
So, naturally, I decided systemd could do everything I needed, and more or less, it can. I've worked through most of the issues I've run into so far, but the latest workaround just feels like a hack.
I have three .service files: one for the DHCP4 daemon, one for the DDNS daemon, and one generic kea.service which is what we use to start/stop the service. What I want to happen is that kea.service will wait for the dependencies to start, and spit out an error if unsuccessful. The best I've been able to come up with was to have kea.service's ExecStart actually sleep and check the output of systemctl is-active for the child services, but, um, that's gross. Kinda like kissing your sister.
I'm hoping someone has more experience with systemd and can offer some guidance on a better approach. Right now, it works, but it just feels really sloppy. Surely there's a better way to do this.
kea.service:
[Unit]
Requires=network-online.target
Requires=mysql.service
After=network-online.target
After=time-sync.target
After=mysql.service
BindsTo=kea-dhcp4.service
BindsTo=kea-dhcp-ddns.service
[Service]
Type=oneshot
ExecStart=/bin/bash -c '/bin/sleep 2 && /bin/systemctl is-active kea-dhcp4 >/dev/null && /bin/systemctl is-active kea-dhcp-ddns >/dev/null'
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
kea-dhcp4.service:
[Unit]
Description=Kea DHCPv4 Server
PartOf=kea.service
[Service]
ExecStart=/usr/local/sbin/kea-start kea-dhcp4
PIDFile=/var/lib/kea/kea.kea-dhcp4.pid
Type=forking
TimeoutStartSec=2
Restart=on-failure
RestartSec=10
RestartPreventExitStatus=0 1
[Install]
RequiredBy=kea.service
The kea-dhcp-ddns.service file is the same as kea-dhcp4.service, just a different PIDFile/ExecStart argument.
Like I say, this does work, just, feels like surely there's a way to do what I'm doing here within systemd config. Right now, calling 'systemctl is-active' in an ExecStart feels like I'm opening the car door by rolling down the window.