Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I've made a programmable power switch using Raspberry Pi (although this question is not RPi specific - it's more of "generic Linux" with problem caused by hardware shortcomings.) Raspberry has no battery-backed RTC; it's intended to work networked and sync its clock soon after boot-up over network.

My problem is, that while I do programming of said switch over the net, and I can get given sockets to switch on/off at given hour that way, the device itself is to be used at different locations, including non-networked ones. When if I carry it from where I programmed it to where it's to be plugged in, it's unpowered and the clock loses state. After I power it back up, it has no connectivity to restore the date.

The few minutes when it's unpowered is not a problem for me - I don't mind the clock being off by a minute or two. I mind if it's off by 43 years as is the case after I switch it on non-networked.

Is there some neat way to restore the clock on boot-up to a state from before the system went down due to power loss? (writing it every second to SD card which is the memory medium of RPi will kill the card quite fast so that's not quite an option.)

share|improve this question

2 Answers 2

up vote 3 down vote accepted

I see a few ways you can approach this

  1. Scan the filesystem for the file with the newest modification or access time. Use that time to set the clock. It's slow, and the accuracy is probably going to be far off, but it'll work. If you have a directory/file you know is modified fairly frequently, you could just use that as the source.

  2. Go with the idea you mentioned; 'touch a file every few seconds'. Many SD cards have wear leveling. So you're not writing to the exact same location all the time, and thus it isn't an issue at all.

  3. Use NVRAM. Write the current date to the NVRAM as often as you want, and then restore that on boot. NVRAM is tiny, but you can store a few bytes in it without issue.

  4. Use GPS for time sync. This is what I do on devices which need time, but don't have access to a network. USB GPS devices are cheap, and they provide very accurate time sources.

share|improve this answer

I suspect the easiest is to actually add a Real Time Clock, like this: http://learn.adafruit.com/adding-a-real-time-clock-to-raspberry-pi/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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