How to write systemd service to ensure start after dns service?

8,428

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
Share:
8,428

Related videos on Youtube

Sraw
Author by

Sraw

Updated on September 18, 2022

Comments

  • Sraw
    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 tried After=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
    Sraw almost 5 years
    Thanks for your answer but it just doesn't work. I am also really confused about this problem...
  • user228505
    user228505 almost 5 years
    can you confirm that you have the Before=nss-lookup.target dependency in your copy of systemd-resolvd.service? Just to be sure and in line with the documentation: freedesktop.org/wiki/Software/systemd/NetworkTarget You did add After=network-online.target and actively request this target by Wants=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.svggives a nice graphical overview.
  • Sraw
    Sraw almost 5 years
    It does work when I change network.target to network-online.target :) I see that network-online.target is a very late target in the startup sequence. BTW, although my service runs after nss-lookup.target all the time, it only works when I change to network-online.target.
  • user228505
    user228505 almost 5 years
    The 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 before network-online.target.
  • Íhor Mé
    Í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
    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é
    Í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
    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