Skip to content

Instantly share code, notes, and snippets.

@tomoto
Last active January 20, 2022 08:05
Show Gist options
  • Save tomoto/2c301abb34f7603e286a141b74993a4d to your computer and use it in GitHub Desktop.
Save tomoto/2c301abb34f7603e286a141b74993a4d to your computer and use it in GitHub Desktop.
Raspberry Pi Shutdown Button
#!/usr/bin/env python
import RPi.GPIO as GPIO
import subprocess
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
while True:
GPIO.wait_for_edge(3, GPIO.FALLING, bouncetime=100)
if GPIO.wait_for_edge(3, GPIO.RISING, timeout=2900) is None:
subprocess.call(['wall.sh', "THE SYSTEM IS SHUTTING DOWN"], shell=False)
time.sleep(3)
subprocess.call(['shutdown', '-h', 'now'], shell=False)
#!/bin/sh
for f in /dev/pts/[0-9]*
do
echo "Broadcast message from $(whoami)@$(hostname) ($(date +'%a %b %d %H:%M:%S %Y')):" > $f
echo $* > $f
done
@tomoto
Copy link
Author

tomoto commented Jan 20, 2022

This modification is made on this article https://howchoo.com/g/mwnlytk3zmm/how-to-add-a-power-button-to-your-raspberry-pi , in order to require the button to be pressed for 3 seconds and broadcast the message to the virtual terminals on GUI before shutting down.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment