Skip to content

Instantly share code, notes, and snippets.

@MoritzBuetzer
Last active May 7, 2020 05:34
Show Gist options
  • Save MoritzBuetzer/294d6d08d60673faddf53a4a26e22dea to your computer and use it in GitHub Desktop.
Save MoritzBuetzer/294d6d08d60673faddf53a4a26e22dea to your computer and use it in GitHub Desktop.
RaspberryPi Shutdown Script
# Add following line to /etc/rc.local
python /home/pi/scripts/shutdown.py &
#!/usr/bin/python
# Connect switch to Pin 5 and 6
import RPi.GPIO as GPIO
import time
import subprocess
GPIO.setmode(GPIO.BOARD)
GPIO.setup(5, GPIO.IN)
oldButtonState = True
while True:
buttonState = GPIO.input(5)
if buttonState != oldButtonState and buttonState == False:
subprocess.call("shutdown -h now", shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
oldButtonState = buttonState
time.sleep(.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment