How to wait for The Internet to be available under systemd

  • Tue 30 November 2021
  • misc

For several years now I've kept a "utility Pi" around - a Raspberry Pi that runs some random housekeeping stuff at my residence. Among other things, it keeps an eye on the UPS, sends dynamic DNS updates (rfc 2136 to my own authoritative DNS servers), and runs Unbound for a recursive DNS server and isc-dhcpd for a DHCP server.

One of the nice things about a Pi is that even an old one boots fast. Really fast. It runs circles around the router and the Juniper EX series switch that it's hooked too.

This creates its own set of problems, since isc-dhcpd will end up in an unhappy state if it can't see the Internet when it comes up. I never got motivated to figure out exactly why that is, but I managed to figure out how to make it wait until the Internet was up before it started isc-dhcpd.

pi@rp-wdbn:/lib/systemd $ cat /lib/systemd/system/isc-dhcpd.service


Description=ISC dhcp server fully from systemd

# Six minute sleep needed by Raspbian 2018-07 because dhcpd gets unhappy if
# the switch isn't up, the router isn't up, the internet isn't up, etc.
# and the Pi sure boots fast after a power outage compared to the Juniper
# switch.  Six minutes seems right for an EX3200, may be able to get away
# with shorter for a 4200 or need more for a 2200.
ExecStartPre=/bin/sh -c 'until ping -c1; do sleep 2; done;'
ExecStart=/usr/sbin/dhcpd -4 -d --no-pid -q -cf /etc/dhcp/dhcpd.conf 
# the following used if Type=forking
# ExecStart=/usr/sbin/dhcpd -4 -q -cf /etc/dhcp/dhcpd.conf 
# ExecStop=/bin/kill `cat /var/run/`
# PIDFile=/var/run/
pi@rp-wdbn:/lib/systemd $ 

Hope someone finds this helpful!