Raspberry PI Shutdown on button press

After adding a button with resistors (there are lots of other resources about that). I wanted a version using systemd.

The gpio testing script is using wiringpi.

Add this as /root/pin-shutdown.sh:


pin=29  # pin 40 (internal: 21) on raspi layout
gpio mode $pin in
gpio wfi $pin both
logger Shutdown button pressed
shutdown -h now

gpio wfi 21 both is blocking until the pin changes its state.

And the systemd service as: /etc/systemd/system/button-shutdown.service:

Description=button shutdown service



Enable the systemd service with: systemctl enable button-shutdown