A very simple systemd service for Raspberry Pi that provides a software-controlled restart / shutdown button. Code: scruss/shutdown_button
Default behaviour is:
- your Raspberry Pi will reset if the button is held for more than two seconds but fewer than five seconds;
- your Raspberry Pi will shut down if the button is held for more than five seconds.
By default, the software assumes the switch is connected to pin BCM 27. Both the pin and the timing can be changed in the Python source file.
- A Raspberry Pi (tested on a model 2B, 3B and Zero)
- A normally open, momentary contact button. I use surplus ATX power buttons (as used on desktop PCs), as they’re cheap and come with a handy set of wires and header connectors. Virtually any button will do the job, though. Just make sure it’s normally open (push to close).
- A Debian-based operating system that uses systemd (tested on Wheezy [RetroPie] and Stretch)
python3-gpiozeropackage to provide GPIO Zero (tested on version 1.4.0)
Connect the button between GPIO 27 and GND. If you use an ATX power button and a Raspberry Pi with a 40-pin GPIO header, connect it across the seventh column from the left:
- · · · · · ·|·|· · · · · · · · · · · · · · · · · · ·|·|· · · · · · · · · · · · · -
The software is installed with the following commands:
sudo apt-install python3-gpiozero sudo mkdir -p /usr/local/bin chmod +x shutdown_button.py sudo cp shutdown_button.py /usr/local/bin sudo cp shutdown_button.service /etc/systemd/system sudo systemctl enable shutdown_button.service sudo systemctl start shutdown_button.service
Enabling the service should produce output very similar to:
Created symlink /etc/systemd/system/multi-user.target.wants/shutdown_button.service → /etc/systemd/system/shutdown_button.service.
You can check the status of the program at any time with the command:
systemctl status shutdown_button.service
This should produce output similar to:
● shutdown_button.service - GPIO shutdown button Loaded: loaded (/etc/systemd/system/shutdown_button.service; enabled; vendor Active: active (running) since Sat 2017-10-21 11:20:56 EDT; 27s ago Main PID: 3157 (python3) CGroup: /system.slice/shutdown_button.service └─3157 /usr/bin/python3 /usr/local/bin/shutdown_button.py Oct 21 11:20:56 naan systemd: Started GPIO shutdown button.
If you’re seeing anything other than Active: active (running), it’s not working. Does the Python script have the right permissions? Is it in the right place? If you modified the script, did you check it for syntax errors?
The output from
dmesg will show you any error messages generated by the service.
If you use a HAT/pHAT/Bonnet/etc. with your Raspberry Pi, check pinout.xyz to see if it uses BCM 27. If you do need to change the pin, best to pick one that doesn’t have a useful system service like serial I/O or SPI. If you’re using an ATX button with a two pin connector, make sure you choose a pin physically adjacent to a ground pin.
If you modify the timing, please ensure that you keep the shutdown button press duration longer than the reboot one. Otherwise you’ll only be able to shut down.
You should not need to reboot to enable the service. One machine of mine — a Raspberry Pi Zero running Raspbian Stretch — did need a reboot before the button worked.
The reboot code is based on the Shutdown button example from the GPIO Zero documentation.
This is not the only combined shutdown/reset button project to use GPIO Zero. gilyes/pi-shutdown also does so, but pre-dates the implementation of the various hold time functions in GPIO Zero.
GPIO 27 was used, as it’s broken out onto a physical button on the Adafruit PiTFT+ display I own.
This is my first systemd service, and I’m still at the “amazed it works at all” stage. The service file may not contain the ideal configuration.