Startside‎ > ‎

UPS for OpenELEC on the Pi

Raspberry Pi with an UPS that runs OpenELEC

How to automatically turn off a Raspberry Pi safely with OpenELEC when the internet fails/ power cuts..

The idea is not mine. I have gathered much inspiration from this site:
Thanks to  Ingeniøren, dgaa1991 and Erik_A at for the code and assistance.

The problem is that the software does not run on OpenELEC and that is why I made this page.

It works like this: When can not access / an IP, it will shutdown the Pi.

I am not a programmer or an engineer. I take absolutely no responsibility for anything that goes wrong !! 

The colour red indicates what needs to be written.

1.) Make a python script that will turn off the Pi when is not accessible. (Here is two scripts, use one of them): 

First use nano to make the file, which will turn off the Pi:


Then write (or copy..) the following (script 1):

import os
import time
import urllib2 

while True:
    except urllib2.URLError:
        os.system("shutdown now -h")    # shutdown command

Press  "ctrl o" to save and then "ctrl x" to exit.

An alternative and more simple script that uses ping instead of opening an URL (script 2):

import os if os.system("ping -c 10") != 0: os.system("shutdown -h now")

Press  "ctrl o" to save and then "ctrl x" to exit.

(OpenELEC do not need sudo before the shutdown command. On other distros they properly do)

2.) Setup cron:

In the console you write the follow command:

crontab -e

.. and write this:

*/5 * * * * python /storage/

Press  "ctrl  o" to save and then "ctrl  x" to exit. Now runs every 5 minutes.

The next command shows you your cron settings:

 crontab -l

If it is looking alright, then you are done with the software setup. 

3.) Hardware setup

Set up your hardware like this, and you are done:

1 x Raspberry Pi, 1 x powerbank which can power the Pi and be charged at the same time: I use the "Cager T10 2600mAh" powerbank, 1 x a switch.
Powerbanks and batteries can be volatile! Do your own research on which powerbank to use.

Terms And Conditions Of
All content provided on this "" site is for informational purposes only. The owner of this site makes no representations as to the accuracy or completeness of any information on this site or found by following any link on this site.
The owner of will not be liable for any errors or omissions in this information nor for the availability of this information. The owner will not be liable for any losses, injuries, or damages from the display or use of this information.