Check if device has synced time since last boot

Hi,

I am trying to fix the following problem:

If a device is down for x hours due to power cutoff and then boots up when the power is up, it takes some minutes to get internet up and time synced with chronyc. I am using the device as a sensor data collector that marks data with a timestamp before sending it to my cloud. This introduces a wrong timestamp on the data points that are read and sent before internet connection is up. (Those messages would then have a timestamp of the device’s last boot session time+local clock time.)

What is the best way to know if the device has synced its time successfully since the last boot, and then get the difference in time and modify the wrong timestamp?

As I want to be able to buffer sensor data locally when the device is offline, choosing to wait for internet connection is not an option for me.

Thanks in advance

Hello @tibhel

You can check to see whether chrony has completed it’s initial synchronisation by using the command chronyc waitsync 1. This command will return a value of 0 when chrony has synchronised. I’m not aware of any method of checking how much the time was adjusted by other than looking in the journal logs:

Nov 23 11:02:39 fc3817f chronyd[1340]: 2020-11-23T11:02:39Z System clock was stepped by 16.676809 seconds

You can also check the initial NTP synchronisation status from within a container using D-Bus. See the Checking NTP sync example here: https://www.balena.io/docs/learn/more/masterclasses/host-os-masterclass/#13-advanced-dbus-examples

Regards

Mark

Thanks for your answer, that seems like the best way yes. Will chrony share the same synchronisation state/time as the Host OS if chrony is installed and checked on a container?
Thanks again

If you use the DBUS mechanism detailed above then it would be the Host Chrony repsonding, so it should be accurate, yes.

Great thanks, should I also mask the service in the dockerfile or not?

Hello,

What do you mean by mask the service in the Dockerfile ?

Are you running systemd in your container ? This is not recommended.

If not, you don’t need to mask anything as there will be nothing to start it.

Right, never mind! Thanks!