This script configures an arbitrary GPIO Pin on a Raspberry Pi computer board to work as a trigger to the /sbin/poweroff
function.
When the power down routine is started
- A message will be printed on
stdout
- A message will be printed into the system log (usually
/var/log/syslog
) - A LED will be turned on until the script is forcefully killed by the OS by the power down sequence.
- This is intended as an Indicator of "Do Not Disconnect the Board". Though the instant the LED turns Off, the SO will still be on working on the shut down sequence.
Installed by default on RaspberryPi OS
python3
systemd
Installed by default on current versions of RaspberryPi OS
- python3-gpiozero package
If not installed, install with: sudo apt install python3-gpiozero
- LED is optional.
- A cable assembly with a Button that can be connected to a GPIO GND Pin and a common GPIO Pin
- Knowledge of the GPIO label number of the selected Pin (board PIN number is different to the GPIO labeled number)
- Place
poweroffbutton.py
on your preferred location. - Edit
poweroffbutton.py
BUTTON_PIN and LED_PIN variables and change them to the selected GPIO Pin number. - Write down the full paths relevant to your environment
- Full path of the directory where
poweroffbuton.py
it's located. - Full path of the python3 binary. Can be discovered with the command
which python3
.
- Edit
poweroffbutton.service
- Change WorkingDirectory parameter to the full path of the directory of the
poweroffbutton.py
script. - Change ExecStart parameter to the correct full path of the python3 binary and the full path of the
poweroffbutton.py
script.
- Install
poweroffbutton.service
into systemd service management service
- Copy
poweroffbutton.service
into/etc/systemd/system/
sudo cp path/of/poweoffbutton.service /etc/systemd/system/
- Enable poweroffbutton service so it automatically starts on system boot.
sudo systemctl enable poweroffbutton
- Start poweroffbutton service
sudo systemctl start poweroffbutton
- You can check enable/disable and start/stop status and the service output with
systemctl status poweroffbutton
- Test!