powerDown is a Raspberry Pi systemd service that watches a UPS power-loss signal on a GPIO pin and shuts the Pi down if power is not restored within a configured grace period.
By default, the script watches BCM GPIO 24. When that pin falls low, it waits 12 seconds, runs sync, waits another 3 seconds for disk flushes, then schedules a normal OS shutdown.
The UPS signal should be connected so that BCM GPIO 24 goes low when mains power is lost.
If your UPS uses another GPIO pin or you need a longer grace period, edit these constants in powerDown.py before installing:
PIN = 24
POWER_OUTAGE = 12
FLUSH_TO_DISK = 3
SHUTDOWN_TIME = "+1"SHUTDOWN_TIME uses the normal shutdown time syntax. +1 schedules shutdown
in one minute, which gives other services time to see the systemd scheduled
shutdown state and close files cleanly. Pi Controller v1.2.3 watches for that
scheduled shutdown and publishes a Watchkeeper power intent for Signal K apps.
On a fresh Raspberry Pi, install Git first:
sudo apt update
sudo apt install -y gitClone this repository:
git clone https://github.com/mcdonaldajr/powerDown.git
cd powerDownInstall the GPIO Python package and set up the service:
sudo apt install -y python3-rpi.gpio
sudo ./install.shThe installer copies the script to /home/pi/powerDown, installs the systemd unit at /etc/systemd/system/powerDown.service, enables it, and starts it.
To install somewhere else:
sudo INSTALL_DIR=/opt/powerDown ./install.shCheck whether the service is running:
sudo systemctl status powerDown.serviceView logs:
sudo journalctl -u powerDown.service -fRestart after editing:
sudo ./install.shStop or disable the service:
sudo systemctl stop powerDown.service
sudo systemctl disable powerDown.servicepowerDown.py- GPIO monitor and shutdown logic.powerDown.service- systemd service template used by the installer.install.sh- installer for Raspberry Pi OS.