Master programming syntax, solve interactive challenges & run live code in Syntaxis Explore Arena →
menu_book Course Curriculum (Raspberry Pi) expand_more
Raspberry Pi / Hardware Projects

safe shutdown button

Learn safe shutdown button in Raspberry Pi with a dedicated explanation, a concept-specific example, practical cautions, and a mastery check.

12 min read • Advanced
raspberry-pi-048.py
python
from gpiozero import Button
from signal import pause
from subprocess import run
button=Button(3,pull_up=True,hold_time=2)
button.when_held=lambda: run(['sudo','shutdown','-h','now'],check=False)
pause()
What to watch
Content source & attribution
Original Syntaxis Content · Original Syntaxis Content