Non-monotonic clock when rebooting with no internet access
Non-monotonic clock when rebooting with no internet access
We are using a RevPi on a product and we do not always have access to the internet. We are finding that when the RevPi is restarted it sets the clock to the last time it had access to the internet. This results in time “going backwards”
An example will help:
2/1/2021 - RevPi is on and has internet connectivity. Time is set correctly.
2/1 -3/1: RevPi is unpowered and shipped to customer
3/1: RevPi is turned on but has no internet access. Date is set to 2/1/2021. While the clock is not correct it continues to move forward and files written on this date appear after files written prior to this date.
3/1-3/2: The RevPi is continuously powered for 48 hours. Clock advances. Time is off by a month but continues to move forward.
3/3: RevPi is rebooted.
-- Prior to rebooting the clock thinks the date is 2/3/2021.
-- After rebooting the clock is set to 2/1/2021. Files written just before rebooting will appear to be 2 days in the future.
Clearly the RevPi records the time to a file while it is synced with NTP. This allows the Pi to boot a month later and still have the date/time advance forward from the last time the system was operating.
Is there a way to tell the Pi to periodically update the time in this file, even when not synced with NTP, such that time never appears to go backwards, even when there is no internet?
More generally, is there a way to ensure that upon booting the clock never moves backwards. (To be reasonable let's say never moves backwards more than 5 minutes.)
If anyone can recommend an RTC module that plugs into the Pi we would welcome that solution too.
Thank you,
Andy
An example will help:
2/1/2021 - RevPi is on and has internet connectivity. Time is set correctly.
2/1 -3/1: RevPi is unpowered and shipped to customer
3/1: RevPi is turned on but has no internet access. Date is set to 2/1/2021. While the clock is not correct it continues to move forward and files written on this date appear after files written prior to this date.
3/1-3/2: The RevPi is continuously powered for 48 hours. Clock advances. Time is off by a month but continues to move forward.
3/3: RevPi is rebooted.
-- Prior to rebooting the clock thinks the date is 2/3/2021.
-- After rebooting the clock is set to 2/1/2021. Files written just before rebooting will appear to be 2 days in the future.
Clearly the RevPi records the time to a file while it is synced with NTP. This allows the Pi to boot a month later and still have the date/time advance forward from the last time the system was operating.
Is there a way to tell the Pi to periodically update the time in this file, even when not synced with NTP, such that time never appears to go backwards, even when there is no internet?
More generally, is there a way to ensure that upon booting the clock never moves backwards. (To be reasonable let's say never moves backwards more than 5 minutes.)
If anyone can recommend an RTC module that plugs into the Pi we would welcome that solution too.
Thank you,
Andy
- p.rosenberger
- Posts: 91
- Joined: 03 Jul 2020, 11:07
Re: Non-monotonic clock when rebooting with no internet access
Hi Andy,
timekeeping is a tough one on embedded devices. We include a RTC in all our products. It is configured and is available as /dev/rtc (/dev/rtc0). But there is a drawback with our RTC it is not battary backed. We use currently a supercap to keep it powered for up to ~24h.
We have some documentation here: https://revolution.kunbus.com/tutorials ... llen-flat/
This is for the RevPi Flat but should work pretty much the same on the other devices as well. For future products we plan to include a battery to power the RTC when the RevPi is not powered.
Best Regards
Philipp
timekeeping is a tough one on embedded devices. We include a RTC in all our products. It is configured and is available as /dev/rtc (/dev/rtc0). But there is a drawback with our RTC it is not battary backed. We use currently a supercap to keep it powered for up to ~24h.
We have some documentation here: https://revolution.kunbus.com/tutorials ... llen-flat/
This is for the RevPi Flat but should work pretty much the same on the other devices as well. For future products we plan to include a battery to power the RTC when the RevPi is not powered.
Best Regards
Philipp
Re: Non-monotonic clock when rebooting with no internet access
The RTC on the Pi does not seem to be used in this case. Rebooting the system results in the RevPi's time being set back in time upon reboot, possibly by several weeks or months. I'm rebooting with "shutdown -r" so the RevPi is being continuously powered while it is restarted.
Re: Non-monotonic clock when rebooting with no internet access
Hi Andy,
With that workaround your time/date should buffered on the RTC again for 24 h.
and post your result.
You have to disable the NTP,
set RTC to UTC
set your time/date manually
(e.g. "2021-04-06 07:30:00)
check the status
Check your time after reboot.
and post it.
Please share your experience with that way.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
up to date you can use the "touch" command.
However, it is not recommended to write constantly, but perhaps once before restarting or shutting down.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
I can't personally give you a recommendation for an external RTC, but maybe someone here in the forum has already had experience with an external RTC.
Best regards
Johannes
With that workaround your time/date should buffered on the RTC again for 24 h.
please check withThe RTC on the Pi does not seem to be used in this case. Rebooting the system results in the RevPi's time being set back in time upon reboot, possibly by several weeks or months. I'm rebooting with "shutdown -r" so the RevPi is being continuously powered while it is restarted.
Code: Select all
sudo timedatectl status
You have to disable the NTP,
Code: Select all
sudo timedatectl set-ntp 0
Code: Select all
sudo timedatectl set-local-rtc 0
Code: Select all
sudo timedatectl set-time "YYYY-MM-DD HH:MM:SS"
(e.g. "2021-04-06 07:30:00)
check the status
Code: Select all
sudo timedatectl status
Code: Select all
date
Code: Select all
sudo timedatectl status
and post it.
Please share your experience with that way.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
To keep the fileClearly the RevPi records the time to a file while it is synced with NTP. This allows the Pi to boot a month later and still have the date/time advance forward from the last time the system was operating.
Is there a way to tell the Pi to periodically update the time in this file, even when not synced with NTP, such that time never appears to go backwards, even when there is no internet?
/var/lib/systemd/clock
up to date you can use the "touch" command.
Code: Select all
sudo touch /var/lib/systemd/clock
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
I can't personally give you a recommendation for an external RTC, but maybe someone here in the forum has already had experience with an external RTC.
Best regards
Johannes
Re: Non-monotonic clock when rebooting with no internet access
Hi Johannes,
Thank you very much for your reply. There is a lot of good information.
touching /var/lib/systemd/clock at some regular interval works well and the system does not "go back in time" more than whatever interval is chosen.
You mention that the file should not be written to constantly. What interval would you recommend? What is the reason for not writing constantly?
--
I would like to enable NTP if it is available, and disable NTP (as you describe) if it is unavailable. It is easy to envision a script that would do this, but I had two questions that will save me time and testing.
To enable ntp are there any steps other than?
When NTP is disabled it is appropriate to replace
with
I realize the RTC would not be set to anything close to the actual time, but at least the RTC would be initialized. My issue with the set-time command is our device is headless and the customer that plugs it in is not equipped to enter the date.
Thank you,
Andy
Thank you very much for your reply. There is a lot of good information.
touching /var/lib/systemd/clock at some regular interval works well and the system does not "go back in time" more than whatever interval is chosen.
You mention that the file should not be written to constantly. What interval would you recommend? What is the reason for not writing constantly?
--
I would like to enable NTP if it is available, and disable NTP (as you describe) if it is unavailable. It is easy to envision a script that would do this, but I had two questions that will save me time and testing.
To enable ntp are there any steps other than?
Code: Select all
sudo timedatectl set-ntp 1
When NTP is disabled it is appropriate to replace
Code: Select all
sudo timedatectl set-time "YYYY-MM-DD HH:MM:SS"
Code: Select all
hwclock -w
I realize the RTC would not be set to anything close to the actual time, but at least the RTC would be initialized. My issue with the set-time command is our device is headless and the customer that plugs it in is not equipped to enter the date.
Thank you,
Andy
Re: Non-monotonic clock when rebooting with no internet access
Hi Andy,
The reason is that the read/write cyclus are limited and therefore my hint.
Maybe I am too cautious
and if you set
you get .
We recommend timedatectl -- > Setting the Date and Time
Best regards
Johannes
I can not recommend any interval, sorry.You mention that the file should not be written to constantly. What interval would you recommend? What is the reason for not writing constantly?
The reason is that the read/write cyclus are limited and therefore my hint.
Maybe I am too cautious
I checked it for you. In my opinion it is enough, because you can check the NTP service withI would like to enable NTP if it is available, and disable NTP (as you describe) if it is unavailable. It is easy to envision a script that would do this, but I had two questions that will save me time and testing.
To enable ntp are there any steps other than?
Code: Select all
sudo timedatectl set-ntp 1
Code: Select all
pi@RevPi47116:~ $ sudo systemctl status systemd-timesyncd.service
● systemd-timesyncd.service - Network Time Synchronization
Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: ena
Drop-In: /lib/systemd/system/systemd-timesyncd.service.d
└─disable-with-time-daemon.conf
Active: active (running) since Fri 2021-04-09 11:12:35 CEST; 1min 15s ago
Docs: man:systemd-timesyncd.service(8)
Main PID: 2549 (systemd-timesyn)
Status: "Synchronized to time server 193.30.121.7:123 (0.debian.pool.ntp.org)."
Tasks: 2 (limit: 4915)
CGroup: /system.slice/systemd-timesyncd.service
└─2549 /lib/systemd/systemd-timesyncd
Apr 09 11:12:34 RevPi47116 systemd[1]: Starting Network Time Synchronization...
Apr 09 11:12:35 RevPi47116 systemd[1]: Started Network Time Synchronization.
Apr 09 11:12:35 RevPi47116 systemd-timesyncd[2549]: Synchronized to time server 193.30.121.7:
Code: Select all
sudo timedatectl set-ntp 0
Code: Select all
Active: inactive (dead)
It is up to youWhen NTP is disabled it is appropriate to replace
withCode: Select all
sudo timedatectl set-time "YYYY-MM-DD HH:MM:SS"
Code: Select all
hwclock -w
I realize the RTC would not be set to anything close to the actual time, but at least the RTC would be initialized. My issue with the set-time command is our device is headless and the customer that plugs it in is not equipped to enter the date.
We recommend timedatectl -- > Setting the Date and Time
But if it works for you, why not?With “timedatectl” you can query and change the system clock and its settings as well as enable or disable services for time synchronization. We recommend “timedatectl” over “hwclock” because it ties to “systemd-timesyncd”, which is enabled by default in Raspbian and thus in the KUNBUS image for the RevPi Flat.
Best regards
Johannes