Devices without RTC, how to store system time

Hi all, I am using Linksys WHW03 as an example but this situation applies to many routers. The device comes without RTC as shown by hwclock but it also doesn't boot without any timestamp at all. It just looks to me the system time is recorded to some unknown place randomly, mostly with a lag about one or a few days.

If I want the system to boot up with more up-to-date system time. What's the correct way to force a persistent system clock update? sysntpd and ntpd commands won't do so.

If you know you intend to issue a reboot, command you can always touch /etc/banner immediately before issuing the reboot. The system will choose the latest timestamp based on the banner file, and will usually be close enough.

Running ntpdate in rc.local pointing at your nations NTP Pool can also be of help.

I usually run a time server on a local network machine with an RTC and point ntpdate at it.

4 Likes

It looks for the newest file in persistent storage and uses that as the initial clock setting. The search includes at least /etc and its subdirectories.

2 Likes

As @RuralRoots said, touching a file before rebooting or shutting down will get you closer to the time for the next boot-up, but it's never going to be perfect.

As a bit of background...

  • Most embedded routers (specifically, effectively all of the all-in-one wifi routers for consumers) lack a hardware RTC.
  • They will sync with the actual time once the system has booted sufficiently as to bring up the upstream interface and underlying network features such that an ntp server can be reached.
  • Prior to the ntp sync, the system will find the most recent time-stamped file and it will use that as its initial time, working up from there until it can 'snap' to the correct time via an NTP sync operation.

So the 'closeness' of the most recently touched/written file will be entirely dependent on the real time that has elapsed between that touch/write operation and the boot cycle in question. That could be mere seconds or it could be days/months/years, depending on the specific situation.

Three things to note:

  1. writing/touching files on the internal flash storage should be approached with caution insofar as the flash memory in these types of devices is not designed for high endurance -- it has relatively limited write cycles. Writing/touching a file before a reboot is probably fine, but don't make a script that touches a file every minute or whatever... that will cause wear and eventually kill the device.
  2. It's never going to be perfect, and there's nothing you can do about that unless you get a device that actually contains a true RTC.
  3. If your reason for needing the correct system time at boot is related to something like a VPN or other feature that requires accurate time to function, you can add a script such that the service will only start after a successful NTP sync, thus avoiding problems such as a VPN deadlock.
3 Likes

Thank you so much guys! It's great to learn the persistent file timestamp effect.

2 Likes

Since I upgrade my devices via CLI, I add the following touch to my login profile, thus ensuring that every time I log in there's a new date stamp (because if it's a manual step, I'll surely forget).

$ grep touch /etc/profile
touch /etc/urandom.seed
4 Likes

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.