Keeping clock current-ish over reboot without ntp

I'm running hardware with no battery backed hardware clock. There is no internet available so no ntp server. I want to be able to reboot the device by cron once a week. Before rebooting, I'd like to save the current time to NVRAM, reboot, and then restore the saved time, and add the number of seconds for typical reboot. I figure this might keep the time reasonably accurate. Any tips on how to make this happen?


At a gross level, you can create a Stop init script to touch a file on /etc (that will happen as it's going down), then look at the timestamp as you're coming up in a separate Start init script, add the required seconds offset, and there you go.

It won't work for power loss (nothing will), it'll never be super-accurate and you probably will never be quite satisfied, but I think that's about as good as it gets.

You don't even have an internal system with internet access/a reasonable time source that you could draw from without the device itself having to reach out over the Internet?

1 Like

The easy alternatives would be pulling the time from a (USB or serial) GPS or DCF77 (or similar in other regions of the world) receiver and feeding that into a local ntpd instance, respectively connecting a simple battery backed rtc over i2c/ spi or similar free GPIO based pins on the board.

At some point, a x86_64 based router (with its battery backed RTC) becomes attractive.

1 Like

Thanks. The system is UPS backed for about 24 hours. The system is totally isolated, so no other device or the internet to provide network time. I'm not a linux expert, so not sure how to do this easily. I know what touch does, so that would create a file as of a specific time. Any tips on how to read the modified time of that file on boot, and add, say, 90 seconds to the time, and set the clock?

I think it would be easier to save the current timestamp just before reboot.

Make your cron job script look like this:

/bin/date +%s >/root/timestamp

Insert the following into /etc/rc.local above exit 0

oldtime=$(/bin/cat /root/timestamp)
newtime=$(($oldtime + $offset))
/bin/date -s @"$newtime" >/dev/null

Play with the offset value ( EDIT: But not less than 70) for best results.


I don't think he's looking to reboot with cron.

Quote from the first post:


Awesome, thank you!

1 Like

Very nice! Epoch +offset :+1:


@pavelgl LOL totally my fault, over time I'd started to confuse it with another thread. Well done, elegant solution.


The U.S. broadcasts a 60mHz radio signal that is sync'd with the NIST. If you like to tinker and the signal is available to you, there are kits:

Also, there are some programs that do exactly what you described in the OP. It does not look like they have been ported to OpenWRT though.


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