Raspberry PI Shutdown Script/Button
In order to do a proper shutdown of a Raspberry PI and avoid corruption of the SD card when running headless (without keyboard or monitor attached); the following python script and a button may be used in combination with an LED indicator that goes off when it is safe to remove the power from the PI.
1.) From a linux Terminal session issue the command:
apt-get install python-dev python-rpi.gpio
2.) Create the file /home/pi/shutdown.py with following content:
import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)
while True:
if (GPIO.input(17) == False):
os.system("sudo shutdown -h now")
break
time.sleep(1)
3.) Edit the file /etc/rc.local and add the following line:
python /home/pi/shutdown.py
Add that line before the exit 0 command.
The above shutdown script is configured to monitor GPIO pin 17 and when pulled low by a reset button issues the system shutdown command. GPIO pin 14 should set up to drive the base of a 2N2222 transistor to switch on an indicator LED.
Shutdown procedure:
Press and Hold the reset button for 2 seconds.
Release reset button
Wait for indicator light to extinguish
Remove power from PI