Keeping an Always-On Project Powered

Covers why sudden power loss corrupts storage, UPS options for small boards, safe shutdown scripts, battery-backed real-time clocks, and testing failover.

Ups battery backup, photographed for a technology article.

A board that runs a clock, a sensor logger, or a small home server is expected to stay up without anyone watching, which makes the mains socket its weakest point. A brief power cut, a tripped breaker, or a pulled plug can corrupt the memory card mid-write and leave the project dead until you reflash it. A small amount of backup power, plus a script that shuts the board down cleanly, turns a crash into a graceful pause. This guide covers why sudden power loss damages storage, the backup options for small boards, shutdown scripts, backup clocks, and how to prove the whole chain works.

Why sudden power loss corrupts storage

The memory card in most small boards is the part that suffers when power vanishes. Writing to flash storage is not instant; the card often holds recent changes in a buffer before committing them, updating internal tables that track where each file lives. If power stops partway through one of these updates, the table is left half-written, so the card no longer knows where files are and the system refuses to boot.

The risk peaks during a write, which is exactly when a database, a log file, or the operating system is saving. A board that writes often, such as a logger recording a reading every second, spends more of its time exposed than one that mostly reads. The always-on jobs this affects appear in the overview of what these boards are used for.

UPS options for small boards

An uninterruptible power supply keeps the board fed for the crucial seconds after mains fails. On a small board you do not need a large desktop unit; several compact options suit the low power draw. The simplest is a power bank that can charge and supply at once, sitting between the mains adapter and the board so the battery covers any gap. Purpose-built add-on boards do the same with a small lithium cell and, crucially, tell the board when mains has failed.

That signal is the difference between a plain battery and a real safety net, because it lets the board know it is on borrowed time and should shut down. Some of these boards communicate over the input and output pins, so the voltages involved matter, a subject the guide on keeping pin voltages safe addresses. A backup that only delays the crash without warning the board buys little beyond a few extra minutes of exposure.

Safe shutdown scripts

A shutdown script turns a warning into a clean stop. When the backup board signals that mains has failed, a small program catches that signal and runs the operating system’s shutdown command, which flushes writes, unmounts the card, and powers down in order.

The script usually watches a pin or listens for a message from the backup board, then waits a short, deliberate delay so it does not react to a flicker lasting a fraction of a second. If mains returns within that window, nothing happens; if it stays off, the shutdown fires. Pairing this with a system that watches the board’s health, described in the notes on keeping an eye on a home server, lets you record every power event and confirm the shutdowns fire when they should.

Battery-backed real-time clocks

Most small boards have no clock that survives a power cut, so when they boot without a network they assume it is whenever they last shut down. For a project that timestamps readings or runs jobs on a schedule, a wrong clock quietly ruins the data. A battery-backed real-time clock, a small add-on with a coin cell, keeps accurate time through any outage and hands the correct time to the board at boot.

This matters most where there is no internet to correct the clock automatically, such as a remote sensor or an off-grid logger. The coin cell lasts years, and the module usually connects over a couple of pins, sharing the header with whatever else the project uses. With the right time on hand, logs stay in order and scheduled tasks fire when they are meant to.

A protection checklist

The pieces above fit together in a fixed order, worth keeping as a short list when building a project.

  1. Choose a backup sized for the board, either a pass-through power bank or a dedicated add-on with a mains-fail signal.
  2. Wire it between the mains adapter and the board so the battery bridges any gap without cutting supply.
  3. Install a shutdown script that watches for the mains-fail signal and calls the shutdown command.
  4. Add a short delay so brief flickers do not trigger an unnecessary shutdown.
  5. Fit a battery-backed clock if the project timestamps data or runs on a schedule without internet.
  6. Cut power under load and confirm the board shuts down cleanly with time to spare.

Testing the failover

An untested backup is really just a hopeful guess. The only way to trust the chain is to pull the mains plug on purpose and watch what happens, ideally several times and under a realistic load. You are checking that the backup takes over without the board resetting, that the shutdown script fires, and that the board powers down cleanly rather than freezing with the card still mounted.

Time the sequence too. Measure how long the battery holds and how long the shutdown takes, then confirm a comfortable margin between them. A board that needs eight seconds to shut down on a battery good for two minutes is safe; one that needs longer than the battery lasts is not.

Where this leaves you

Protecting an always-on board is a chain, and it is only as strong as its weakest link. A battery with no shutdown script merely delays a hard crash; a script with no battery never gets the chance to run; a perfect setup that has never been tested is a hopeful guess. The three parts, backup power, a clean shutdown, and a test that proves them, work only together.

None of it is expensive or difficult, and a modest add-on board plus a short script covers most projects. An always-on board has to stay cool as well as fed, the concern the guide on managing board temperature handles, and the two protections belong together in any build meant to run untouched for years. Set it up once, prove it by cutting power, and an outage becomes a pause instead of a rebuild.

Frequently asked questions

How do I protect a Pi from power cuts?

Add a small backup power source and a shutdown script. A pass-through power bank or a dedicated add-on board keeps the Pi running for the seconds after mains fails, and a script that detects the failure shuts it down cleanly before the battery empties. This prevents the mid-write corruption a sudden cut causes. Test it by pulling the plug and watching for a clean shutdown.

Do I need a UPS for a home server?

If the server stores data or runs constantly, a small backup is well worth it. Home mains suffers brief dips and cuts that can corrupt a memory card mid-write, forcing a full reflash. A compact battery add-on costs little and, paired with a shutdown script, removes that risk. A board that runs occasionally and holds nothing important can reasonably skip it.

How do I make a Pi shut down safely on power loss?

Use a backup that signals when mains fails, then run a small script that watches for that signal and calls the shutdown command. Add a few seconds of delay so brief flickers are ignored. The shutdown flushes pending writes and unmounts the card before the battery runs out. Size the battery to outlast the shutdown, then test the sequence by cutting power.