A script for initiating a safe shutdown on a Raspberry Pi when a button, attached to GPIO pins, is pressed.
- Get the git repository:
git clone https://github.com/johnpeart/raspberrypi-safeshutdown
- Change the permissions as the super-user such that you can execute the files:
sudo chmod 777 /home/pi/raspberrypi-safeshutdown
- Open
rc.local
in the text editor as the super-user:
sudo nano /etc/rc.local
- Add the following command before the
exit 0
at the end ofrc.local
:
sudo python /home/pi/raspberrypi-safeshutdown/safe-shutdown.py &
- Reboot the Raspberry Pi:
sudo reboot
NOTE: It is very important that you add the code in step 4 before the
exit 0
. If you don't, you'll cause terminal havoc.
You will need an "Normally Open" ("NO") switch and some wire. You should:
- attach a wire to each of the terminals on the button
- attach the other ends of the wire to GPIO 3 and a GND pin (e.g. physical pins 5 and 6)
The benefit of using GPIO 3 is that it will also power on the Raspberry Pi when shorted to a GND pin.
Based on tutorial from Core Electronics