1

Is it possible to programmatically know if systemd-timesyncd;

  • Can access to the NTP servers?
  • Has synced the system time with the server?

The reason is I have an application depends on precise time information and must be started after system time is updated. Or is there any other method without using systemd?

Thanks.

0

2 Answers 2

2

From this link ( systemd-timesyncd.service man page):

/var/lib/systemd/timesync/clock

The modification time ("mtime") of this file indicates the timestamp of the last successful synchronization (or at least the systemd build date, in case synchronization was not possible). It is used to ensure that the system clock remains roughly monotonic across reboots, in case no local RTC is available.

/run/systemd/timesync/synchronized

A file that is touched on each successful synchronization, to assist systemd-time-wait-sync and other applications to detecting synchronization with accurate reference clocks.

1
  • Good answer. But to actually access that info, you would the do: date -r /run/systemd/timesync/synchronized or stat -c %y /run/systemd/timesync/synchronized. Jan 25, 2021 at 14:06
0
 systemctl status systemd-timesyncd
 * systemd-timesyncd.service - Network Time Synchronization
   Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2019-10-25 11:36:40 CEST; 52min ago
     Docs: man:systemd-timesyncd.service(8)
 Main PID: 881 (systemd-timesyn)
   Status: "Synchronized to time server 91.189.91.157:123 (ntp.ubuntu.com)."
    Tasks: 2 (limit: 4915)
   CGroup: /system.slice/systemd-timesyncd.service
           `-881 /lib/systemd/systemd-timesyncd

Oct 25 11:36:40 computername systemd[1]: Starting Network Time Synchronization...
Oct 25 11:36:40 computername systemd[1]: Started Network Time Synchronization.
Oct 25 11:37:11 Computername systemd-timesyncd[881]: Synchronized to time server 91.189.91.157:123 (ntp.ubuntu.com).
4
  • Hmm. I believe I should be a bit more clear. I'm looking for a way to programmatically get that info. Do you suggest parsing the output? Oct 25, 2019 at 10:52
  • No, not really. I can not script. But maybe you can start your application over a systemd.service bound to systemd-timesyncd.service .
    – nobody
    Oct 25, 2019 at 11:12
  • 2
    But still, it's not possible to know if the time is synced or not, right? Is there any state of the systemd-timesyncd to tell waiting services that time has synced? Oct 25, 2019 at 16:06
  • @benjaminbutton Well you have 2 choices: 1-writing a method to monitor the file changes every 1 or 2 seconds 2-execute a bash script and parse returned string. if you are seeking an instant notification it is not possible IMO. Maybe sb has a better solution.
    – Behzad
    Jan 25, 2021 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.