This is extracted from a single post by user RonR in a thread about FreePBX for the Raspberry Pi on DSLReports. I though it might be useful to many Raspberry Pi users:
Raspberry Pi Pushbutton Shutdown/Startup
The attached script will install shutdown and startup capability using a SPST NO momentary pushbutton
switch connected to GPIO header pins 5 and 6. Pressing the button on a running system will initiate
a graceful shutdown (shutdown -h now). Once shut down, pressing the button will restart the system.1. Copy install and gpio-shutdown.dtbo to the Raspberry Pi.
2. Make the install script executable:
$ chmod +x install
3. Run the install script:
$ sudo ./install
4. Reboot
For additional info, see:
http://www.stderr.nl/Blog/Hardware/RaspberryPi/PowerButton.html
The script can be downloaded from RonR’s original post or directly from https://www.dslreports.com/r0/download/2324182~6a03744946c51bfa4876b6ecd8d4528c/PushButton.zip