7

How can I recognize which service provides time-sync.target in systemd? Is it configurable or is it hardcoded in systemd? How can I configure my custom time-sync daemon/service to provide this target?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250

1 Answers1

11

Run

systemctl show time-sync.target

to see the information and configuration maintained by systemd for this target. In particular, this will tell you that it is part of the special systemd units and documented there:

Services responsible for synchronizing the system clock from a remote source (such as NTP client implementations) should pull in this target and order themselves before it. All services where correct time is essential should be ordered after this unit, 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 "$time" facility.

It is fully configurable (use systemctl edit to override any aspect you want to). As mentioned above, services which provide time synchronisation (e.g. systemd-timesyncd.service) declare that they want this target, and order themselves before it; services which need time synchronisation order themselves after it.

To configure a time synchronisation service, copy the relevant parts of systemd-timesync’s configuration:

[Unit]
Before=time-sync.target
Wants=time-sync.target
Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
  • `Services... should pull in this target` means `Starting the services... should trigger the starting of this target`? – TheMeaningfulEngineer Mar 21 '19 at 13:14
  • Pretty much, yes; it means that services ... should declare `Wants=time-sync.target`, which has the effect of starting `time-sync.target` when the configuring services are started (but not requiring the target, *i.e.* the transaction won’t fail if `time-sync.target` fails). – Stephen Kitt Mar 21 '19 at 13:16
  • And `time-sync.target` only has a ordering dependency on `systemd-timesyncd.service` (`systemd-timesyncd.service` starts before `time-sync.target`). If `systemd-timesyncd.service` fails soon after it started, `time-sync.target` is still reached? – TheMeaningfulEngineer Mar 21 '19 at 13:19
  • That depends on exactly when it fails. If it fails during startup, `time-sync.target` will not be started; if it fails after startup, `time-sync.target` will have started too. `systemd-timesyncd.service` is configured to restart on failure, with no delay, so if it fails, systemd will restart it immediately. – Stephen Kitt Mar 21 '19 at 13:32
  • `If it fails during startup` - what do you mean with `startup` here? Would a segfault in `systemd-timesyncd.service` be a failed startup? (trying to understand what would be considered the threshold from "starting up" to "started") – TheMeaningfulEngineer Mar 21 '19 at 13:57
  • “Startup” is everything that happens as a result of `ExecStart` and related directives, interpreted according to the service’s type. In `systemd-timesyncd`’s case, that’s running the `systemd-timesyncd` binary, and since it’s a notify-type service, waiting for it to notify that it has started up correctly. – Stephen Kitt Mar 21 '19 at 14:04