User avatar
jbeale
Posts: 3375
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

when after startup is the time correct?

Sun Apr 14, 2019 9:02 pm

I want to start a process logging data on my R-Pi when it boots up. However, the data must be accurately stamped with the current time, which can only happen after the Pi has updated its clock from the network. If I run "timedatectl status" from either @reboot in crontab or the /etc/rc.local file and redirect the output to a file, I see something like this (note: "NTP synchronized:no")

Code: Select all

      Local time: Sun 2019-04-14 13:51:59 PDT
  Universal time: Sun 2019-04-14 20:51:59 UTC
        RTC time: n/a
       Time zone: America/Los_Angeles (PDT, -0700)
 Network time on: yes
NTP synchronized: no
 RTC in local TZ: no
At some later time after bootup, the NTP sync status changes to "yes":

Code: Select all

      Local time: Sun 2019-04-14 13:58:06 PDT
  Universal time: Sun 2019-04-14 20:58:06 UTC
        RTC time: n/a
       Time zone: America/Los_Angeles (PDT, -0700)
 Network time on: yes
NTP synchronized: yes
 RTC in local TZ: no
Is there any better way to determine when the clock is good after bootup than repeatedly issuing this command and parsing the output to see if the "NTP synchronized" line ends with "yes" ?

EDIT: In my system, it looks like "NTP synchronized" changes from "no" to "yes" between 30 seconds and 1 minute after startup.

W. H. Heydt
Posts: 9990
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: when after startup is the time correct?

Sun Apr 14, 2019 11:58 pm

Add a good RTC?

Andyroo
Posts: 2833
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: when after startup is the time correct?

Mon Apr 15, 2019 12:08 am

I would think it is after the systemd-timesyncd.service connects to a time server and gets a valid response.
Need Pi spray - these things are breeding in my house...

Silverlock
Posts: 83
Joined: Wed Apr 10, 2013 3:51 am

Re: when after startup is the time correct?

Mon Apr 15, 2019 12:14 am

This shell script scans the tail end of the syslog file for notification that the NTP time synch event has occurred. I found it to be much quicker than waiting for timedatectl to discover that it's happened.

Code: Select all

#!/bin/bash
tail -50 /var/log/syslog|grep -q "Synchronized to time server"
status=$?
while [ $status -ne 0 ]
do
    sleep 1
    tail -50 /var/log/syslog|grep -q "Synchronized to time server"
    status=$?
done
# your code here...
RPi3+external HDD/Raspbian Stretch

User avatar
jbeale
Posts: 3375
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: when after startup is the time correct?

Mon Apr 15, 2019 2:26 am

Thanks Silverlock. In my case it looks like I have different messages because I'm using 'chronyd' instead of NTP. Looks like I want to check for "Started Time & Date Service" given this syslog snippet:

Code: Select all

Apr 14 14:05:28 rp48 chronyd[419]: System clock wrong by 8.680650 seconds, adjustment started
Apr 14 14:05:28 rp48 systemd[850]: Time has been changed
Apr 14 14:05:28 rp48 chronyd[419]: System clock was stepped by 8.680650 seconds
Apr 14 14:05:28 rp48 systemd[1]: Time has been changed
Apr 14 14:05:28 rp48 systemd[1]: apt-daily-upgrade.timer: Adding 37min 9.447697s random time.
Apr 14 14:05:28 rp48 systemd[1]: apt-daily.timer: Adding 7h 21min 42.913780s random time.
Apr 14 14:06:34 rp48 chronyd[419]: Selected source 2606:c680:0:b:3830:34ff:fe66:6663
Apr 14 14:06:40 rp48 dbus[303]: [system] Activating via systemd: service name='org.freedesktop.timedate1' unit='dbus-org.freedesktop.timedate1.service'
Apr 14 14:06:40 rp48 systemd[1]: Starting Time & Date Service...
Apr 14 14:06:40 rp48 dbus[303]: [system] Successfully activated service 'org.freedesktop.timedate1'
Apr 14 14:06:40 rp48 systemd[1]: Started Time & Date Service.
I modified your code example accordingly and it looks like it works; thank you!

Return to “General discussion”