How to write systemd service to ensure start after dns service?
Please add a dependency in your service file:
After=nss-lookup.target
This should ensure that host/name lookup is operable.
Further details are in the documentation: https://www.freedesktop.org/software/systemd/man/systemd.special.html
nss-lookup.target
A target that should be used as synchronization point for all host/network name service lookups. Note that this is independent of UNIX user/group name lookups for which nss-user-lookup.target should be used. All services for which the availability of full host/network name resolution is essential should be ordered after this target, but not pull it in. systemd automatically adds dependencies of type After= for this target unit to all SysV init script service units with an LSB header referring to the "$named" facility.
To fully wait for network, the documentation of systemd https://www.freedesktop.org/wiki/Software/systemd/NetworkTarget/ directs you to add to your service:
After=network-online.target
Wants=network-online.target
and to confirm that the relevant "wait" service is enabled:
$ systemctl is-enabled NetworkManager-wait-online.service systemd-networkd-wait-online.service
disabled
enabled
Related videos on Youtube
Sraw
Updated on September 18, 2022Comments
-
Sraw over 1 year
After upgrading to Kubuntu 19.04, I don't know how to write a service so that it can start after DNS service has been initialized.
In this past, I just use
After=network.target
, but now it doesn't work. I also triedAfter=network-online.target
, but no luck.So I think the problem is
systemd-resolved
, how could I solve this problem?
update:
ERROR: getaddrinfo: Temporary failure in name resolution ERROR: failed to resolve the provided hostname
These are two related logs. So I can confirm the problem is related to DNS service.
-
Sraw almost 5 yearsThanks for your answer but it just doesn't work. I am also really confused about this problem...
-
user228505 almost 5 yearscan you confirm that you have the
Before=nss-lookup.target
dependency in your copy ofsystemd-resolvd.service
? Just to be sure and in line with the documentation: freedesktop.org/wiki/Software/systemd/NetworkTarget You did addAfter=network-online.target
and actively request this target byWants=network-online.target
and it still does not have network when your service starts? Can you double-check the startup sequence?systemd-analyze plot >startup.svg
gives a nice graphical overview. -
Sraw almost 5 yearsIt does work when I change
network.target
tonetwork-online.target
:) I see thatnetwork-online.target
is a very late target in the startup sequence. BTW, although my service runs afternss-lookup.target
all the time, it only works when I change tonetwork-online.target
. -
user228505 almost 5 yearsThe documentation of systemd hides this a bit.
nss-online.target
allows your service to communicate with the resolver. In case you need the network to be up and running to have the resolver contacting other systems then the network needs to be running. For this purpose exists a one-shot service which waits (long) for the network.systemctl cat systemd-networkd-wait-online.service
shows details of this service, which is ordered beforenetwork-online.target
. -
Íhor Mé almost 4 years
systemctl is-enabled NetworkManager-wait-online.service systemd-networkd-wait-online.service
->Failed to get unit file state for NetworkManager-wait-online.service: No such file or directory
on Ubuntu 20.04 -
user228505 almost 4 years@ÍhorMé please check your installation. Maybe you missed to install a package. Here is a link to the relevant man-page: manpages.ubuntu.com/manpages/focal/en/man8/…
-
Íhor Mé almost 4 years@user228505 Maybe, it's just not present on 20.04 anymore. I do seem to have it on 18.04, but not on 20.04.
-
user228505 almost 4 years@ÍhorMé version 20.04 is focal. Package list shows this file at least in amd64 architecture: packages.ubuntu.com/focal/amd64/network-manager/filelist