This setup adds a momentary button that requests an orderly Raspberry Pi OS shutdown and can wake a halted Pi while power remains connected. It uses the kernel gpio-shutdown device-tree overlay and systemd-logind; no continuously running Python service is required.
The default wiring uses GPIO3 because Raspberry Pi firmware supports wake from halt when GPIO3 is pulled low.
Use a normally-open, momentary push button:
- One terminal to physical pin 5, GPIO3 (BCM numbering)
- The other terminal to physical pin 6, ground
GPIO3 has an external pull-up on supported boards. Pressing the button connects GPIO3 to ground.
Do not connect the button to 3.3 V or 5 V. Confirm the header orientation and pin numbers for the exact Raspberry Pi model before wiring. Power down and disconnect power while changing wiring.
GPIO3 is also I2C1 SCL. Do not use this button arrangement when the same pin is required by an I2C bus or attached hardware. Raspberry Pi 1 Model B revision 1 uses GPIO1 for this function and is outside this guide's default configuration.
- Pressing the button generates a
KEY_POWERevent; systemd-logind requests shutdown. - Pressing it after halt can wake the Pi while its power supply remains connected.
- This is a shutdown request, not a physical mains or DC power disconnect.
- A severely wedged kernel or blocked shutdown can prevent an orderly halt.
- Applications must still save and flush important data correctly.
- Button behavior can be changed by logind configuration, another input-event handler, an incompatible operating system, or conflicting overlays.
- Raspberry Pi models with a dedicated onboard power button may not need an external GPIO3 button.
- A supported Raspberry Pi with a 40-pin header
- Current Raspberry Pi OS using systemd-logind
/boot/firmware/config.txt- Root access to change the boot configuration
- Bash,
awk,curl,install, and standard core utilities for the helper
This revision-free command installs the current helper as /usr/local/bin/rpi-shutdown-button:
curl -fsSL https://gist.githubusercontent.com/lbussy/9e81cbcc617952f1250e353bd42e7775/raw/shutdown_button.sh | sudo bash -s -- --installThis deliberately skips review and follows the current unpinned Gist revision. Anyone controlling the served content can execute code as root. Use it only when you accept that risk; otherwise use the inspect-first procedure below.
Installing the helper does not change config.txt and does not reboot the Pi.
This revision-free URL always retrieves the current helper:
curl -fsSL https://gist.githubusercontent.com/lbussy/9e81cbcc617952f1250e353bd42e7775/raw/shutdown_button.sh -o shutdown_button.sh
less shutdown_button.sh
bash -n shutdown_button.sh
sudo install -m 0755 shutdown_button.sh /usr/local/bin/rpi-shutdown-button
rm -f shutdown_button.shBecause the URL is unpinned, its content can change. Inspect it before installation.
rpi-shutdown-button --statusThe command reports the selected boot configuration, whether its managed block is absent, present, or malformed, and how many active gpio-shutdown entries it finds.
The helper fails closed when managed markers are malformed or an unmanaged gpio-shutdown entry would conflict.
Preview the exact addition without writing:
rpi-shutdown-button --enable --dry-runEnable the button:
sudo rpi-shutdown-button --enableThe helper:
- verifies that the configuration is an existing regular, non-symlink file;
- refuses conflicting or duplicate active
gpio-shutdownentries; - creates a timestamped metadata-preserving backup;
- writes through a same-directory temporary file;
- adds an explicitly marked configuration block;
- makes no change when its exact configuration is already present;
- never reboots automatically.
Reboot when ready:
sudo systemctl rebootIf you do not install the helper, back up /boot/firmware/config.txt and add this block:
# BEGIN rpi-shutdown-button
dtoverlay=gpio-shutdown,gpio_pin=3,active_low=1,gpio_pull=up,debounce=100
# END rpi-shutdown-buttonThe parameters explicitly select GPIO3, active-low behavior, pull-up bias, and 100 ms debounce. GPIO3, active-low, pull-up, and 100 ms are also the overlay defaults, but spelling them out makes the intended electrical behavior reviewable.
The Gist's config.txt contains the same sample block.
Confirm the configuration and input device:
rpi-shutdown-button --status
grep -A 8 -B 2 -i 'gpio.*key\|power button' /proc/bus/input/devices || true
sudo journalctl -b | grep -Ei 'gpio|power key|button' || trueTest during a maintenance window with local access and no important unsaved work:
- Press and release the button once.
- Confirm that Raspberry Pi OS performs an orderly shutdown.
- Wait until shutdown completes.
- Press and release the button again to test wake from halt.
Do not repeatedly press the button during shutdown or disconnect power before shutdown completes.
Preview removal:
rpi-shutdown-button --disable --dry-runRemove only the helper-managed block:
sudo rpi-shutdown-button --disable
sudo systemctl rebootThe helper creates a timestamped backup before removal and leaves unmanaged configuration untouched. If the managed markers are damaged or incomplete, it refuses to edit the file and requires manual review.
The helper defaults to /boot/firmware/config.txt. For an operating system that keeps its active boot configuration elsewhere, provide an absolute path explicitly:
sudo RPI_CONFIG_PATH=/path/to/config.txt rpi-shutdown-button --enableUse this only after confirming that the selected file is the configuration actually read during boot.
A simple LED driven by a normal GPIO can indicate that software has reached a chosen state, but it does not prove that writes are complete or that power is safe to remove.
The gpio-poweroff overlay is intended to signal an external circuit that physically removes power. It interferes with the normal power-down sequence, disables GPIO3 wake, and requires external power-control hardware to act on its signal. Do not use it merely to drive an indicator LED.
If a status indicator is required, design it separately around the exact meaning needed—application ready, operating system running, shutdown requested, or external power safe—and document that it is not a substitute for verified shutdown.
- Incorrect wiring can short power rails or damage the Pi.
- GPIO3 conflicts with I2C1 SCL.
- A stuck or electrically noisy button can cause unwanted shutdown requests.
- Shutdown does not remove supply voltage from the board or attached hardware.
- Overlay behavior depends on firmware, kernel, board, and operating-system support.
- Backups of
config.txtremain on the boot filesystem and should be managed appropriately. - Test the complete shutdown and wake behavior before relying on it remotely.
https://forums.raspberrypi.com/viewtopic.php?t=363725
Looks like you need to click twice. Odd, but hey.