Obviously I'd need a UPS or battery of some kind to continue supplying power during an outage to my home. However, I'd like to have the Pi automatically shut down safely if it is switched to backup power so that sudden power loss doesn't damage the pi or the SD card (I've lost a card that way before). If I'm present, I could send a shutdown command -- except I don't usually have a keyboard attached, preferring to do my administrating through PuTTY, and my router doesn't function without power either. What are my options?

  • Poking around some, I found this similar question on Electronics: electronics.stackexchange.com/questions/61877/… Unfortunately most of that is greek to me, but I'll be consulting with an engineer friend of mine, so that'll probably help him. Jul 9, 2013 at 16:05
  • 1
    If you are using a UPS that is made for a destop PC, you pi will be able to run for days, before the battery is empty. Just my 2 cents.
    – Gerben
    Jul 10, 2013 at 10:26

4 Answers 4


There's software in the repository exactly for this purpose:

$ apt-cache search apcupsd
apcupsd - APC UPS Power Management (daemon)
apcupsd-cgi - APC UPS Power Management (web interface)
apcupsd-doc - APC UPS Power Management (documentation/examples)

The correct solution to this common problem would be getting a good APC UPS, connecting all your network setup (routers, PI, other servers maybe) to the UPS and connecting the UPS to RasPi using plain simple USB cable. After that you may install apsupsd, that will watch the power conditions and issue shutdown command at the appropriate moment.

Other than that, this setup provides a wonderful opportunity to watch how your mains voltage changes over the time (using rrdtool), the numbers on the left are Volts:

voltage changing over the last 24 hours


Like you said, you will need a battery to support the pi after the outage. After you have that setup, take a GPIO pin, and wire that up so it has 3.3v to it when the power is on, and 0v to it when the power is off. Make sure you include a pull down resistor to force that pin to 0v when no current is applied. There are a number of ways to do this, but a simple regulated wall wart is probably the simplest, but you may be able to take something off your battery charging circuit as well. I'll let you research the circuitry on your own first, but if you need more direction I would be happy to help.

From there, you will need to write a script in the language of your choice that when that pin is taken down to 0v for X Seconds/Minutes, etc, will run the shutdown command. Start this script on boot with a CRON job (After testing of course).
Again, I'll let you research on your own first, but would be happy to provide more advice if necessary.


A low cost solution is to use a USB battery pack. A decent battery can power a pi for a number of hours depending on usage and battery capacity. Here's an example of a DSL modem charging a battery that is powering a pi. In the example, the modem can reset and the pi won't get interrupted.

The battery needs to be rated at 5v and have a high enough input and output amperage to remain charged and power a pi respectively. Bigger capacity batteries will supply power longer.

enter image description here


Sorry to answer this with a link, but it's more involved than will fit in a simple Stack Exchange answer. You can use a standard, off-the-shelf UPS, and the Network UPS Tools (NUT) package to allow a Pi to shut itself down when the power goes out. I have just posted an article I wrote with step-by-step instructions for setting it up:


I hope that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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