Making pi robust to power outages

I’m aware of the issues with just cutting the power to a pi, and some of the ways of getting around it in hardware (i.e. on/off switches).

I was curious though if there were ways to make a pi less sensitive to power cuts in software alone? By reducing disk writes to nil (or almost)?

Instinctively this feels like a question that would have been addressed by people making embedded devices but I’d be interested in whether anyone had a raspian configuration or an alternative distribution made with this problem in mind


Devil’s Advocate: So what good is a power switch? unless it’s software-controlled, a power switch is the same as pulling the plug, there is no polite shutdown. and if you have console/ssh/etc, just typing ‘shutdown’ IS the power switch… so we need more details.

As always it’ll depend on your build/project/application. If you truly want it to be “safest”, your best bet is to run from a high-capacity battery-based supply that’s constantly being charged… aka the “laptop” or “U.P.S.” scenario. is this an embedded use, treating the pi like a microcontroller? Or is this an interactive computer with a full UI/GUI scenario?

For one example, building a live system instead of an “installed” system. Your system becomes essentially read-only; a good way to run for say a car-based media player or boom box scenario. In the debian live tools, there tools like live-toram, that do exactly what you’d think, takes the running live system and gets everything off the media into ram, so even the thumb drive or microsd you booted from can be ejected with no harm. Of course RAM disks are nothing new, and probably easier. Now if this is an interactive system, then your persistence file/partition will need to be set up properly and not system-wide, but with tweaking and some trial/error you can minimize damage of an unintended power loss to just the user files being written at the time.

I’m sure if you head over to the RPi forum, this question has been asked/answered dozens of times since v1.0 Pi’s. A Raspbian based starting point might be:

good luck!

1 Like

Thanks for the response. My point about hardware/software was that whilst a button can be set up to run a shutdown switch, it cannot turn on the pi without additional hardware attached as far as I can see.

If the distro is run “live” a simple power toggle switch would not pose a problem (as the disk is treated as read only). I’ve not thought about this for a while (used to make knoppix variants) but I’ll have a read- thanks!

hey again. you are welcome, however your info on power button is, well…

A simple momentary SPST pushbutton wired to pins 5 & 6 are all you need for a power switch, and a bit of code to monitor the pin. First press generates a proper ‘shutdown’ call, and once it’s down, the next press generates a reset. Well, yea, technically that is “additional hardware” but not like a new board to purchase. Some people worry about I2C being on those pins, but they are not electronics-minded individuals. You are either starting or stopping, so it will hurt nothing, in fact pulling a high line low is exactly how I2C communication occurs. :-)

Again, I guess your robustness to “power outages” will simply depend on what you want and why it matters. I mean with a big enough capacitor, say a 10F … not 10uF, but ten farads… (think coke can) or 100F (think Foster’s lager can)… is essentially a battery backup. One passive component. Although, massive. The competitive car stereo crowd in America loves these, because their ludicrous amplifiers pull more amperage than a MIG welder, and put such a strain on the vehicle’s electrical system. You can find them on eBay etc for cheap. I think Adafruit is selling a 700!!! Farad 2.5V, imagine a quad of those monkeys! You could run your pi with an LCD and a ton of USB peripherals, for days… no battery.

Point being, there’s many options. If it’s just about preventing corruption on your sdcard from intermittent power issues, just moving the running OS into RAM should be plenty.

1 Like

Sorry for not being clearer- it’s the on component of the on/off switch I’m interested in. That is to say, switching it on without unplugging.

That said I’m more curious in a general sense, particularly about live distributions. I’m planning a project (a toy with switches and lights) but I’ve also been thinking about controlling power a lot in light of the ominous warnings about just unplugging the pi. I agree about moving the OS to RAM and reading up now!