I've noticed that if I set a date through the timedate command, let's say

date -s "2 OCT 2006 18:00:00"

and I reboot the Raspberry or I do a proper shutdown the system hold the right date, at least with an error given by the time the Pi has been down for. Instead on a direct power off the system restore the date of last proper shutdown or reboot. Is there a way to keep the date present at power supply lack time?

  • Any device that needs a valid time will use a battery backed up RTC(real time clock). This chip will be powered by the small battery for up to a few months even if the device is not plugged in. Then on startup, time is read from the RTC and used to set the system time. You can do this yourself on the pi if you want. there are many options available. – Chad G Sep 15 '20 at 16:43
  • The boot scripts take the time the root filesystem was last unmounted to set the time. But when the system is just turned off then the time isn't updated in the filesystem and you get the time of the last clean shutdown instead. The reason this is done is more to avoid errors that the filesystem has a timestamp in the future than to set a correct time. You should use NTP to get an accurate time over the network (systemd-timed, ntpd, chrony can do that). – Goswin von Brederlow Sep 27 '20 at 16:09

Is there a way to keep the date present at power supply lack time?

The issue with improper shutdown1 is that logically, it cannot be predicted. At all. When the power is cut, the power is gone. There is no warning or few microseconds with some electricity left.

That's a physical fact. It is not subject to argument, some hidden loophole, etc. It is like gravity. There are of course things that can be done to mitigate the possibility (see footnote), but these all, without exception, require additional hardware.

Sans that, what you could do is implement your own service which:

  • Writes the time to a file periodically, eg., every minute.
  • At startup, reads that file (including some kind of sanity check in case the file is corrupted because the write was interrupted by the power cut) and sets the system time if there is no network connectivity, since if there is the time will be correctly set that way.

You'll have to experiment to supersede whatever it is that sets the date currently.

  1. This affects pretty much all electronic devices with a microprocessor, not just the Pi. However, it is less likely to happen to most of your ready-made end-consumer goods because of how they are made -- e.g., smartphones will shutdown cleanly before their battery is gone.
  • Ok as I thought, but is there a way to save at least the date at the moment i give the timedate command? – Antonio Del Sannio Sep 16 '20 at 7:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.