How can I configure a custom systemd service to start only after DNS resolution is ready to be handled?
The service is a oneshot script that mails me the current WAN and LAN ip addresses of the device, just after booting.
It used to work, but after installing Pi-hole, it stopped working when run automatically at boot. Triggering the service manually with sudo systemctl start mail-ip-at-boot.service
still works.
At boot, it's failing because there is an issue with name resolution (of e.g. the mail server's host name). From /var/log/syslog
:
sendemail[1182]: ERROR => Connection attempt to smtp.<myisp>:25 failed IO:Socket::INET6 getaddrinfo: Temporary failure in name resolution.
I had a similar issue reported by dig
(which I use in my mailip script): dig: couldn't get address for 'resolver1.opendns.com': failure
. I already solved that one by replacing the resolver1 hostname with its fixed IP address. Of course, it's only a workaround.
The solution should work regardless of whether Pi-hole is installed or not.
Below is my current service definition.
me@raspberripi:~$ cat /etc/systemd/system/mail-ip-at-boot.service
[Unit]
Description=Mail WAN and LAN IP address
Wants=network-online.target
After=network-online.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/mailip.sh
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
Temporary workaround:
- Approach 1 (not working) I thought I found a good solution in using the
nss-lookup.target
. It is described as "A target that should be used as synchronization point for all host/network name service lookups." (see https://www.freedesktop.org/software/systemd/man/systemd.special.html). Used e.g. with the directive:
Wants=network-online.target nss-lookup.target
After=network-online.target nss-lookup.target
Although looking very pormising, approach 1 doesn't seem to work.
- Approach 2. I got it working by referring to the
pihole-FTL.service
. It appears (I have not tested it) that because of theWants
directive, it will still work if pihole-FTL is not available. See https://unix.stackexchange.com/a/423724/40237
Wants=network-online.target pihole-FTL.service
After=network-online.target pihole-FTL.service
I don't like approach 2 entirely, as I create coupling to pihole. Not good practice