Setting up an OS service to shutdown the Raspberry Pi requires two short scripts and registering the shell to run at startup:

  1. A short python script to
    • Set up a GPIO pin to wait to be pulled to be grounded
    • When grounded, run the shutdown command
  1. A shell script to set the python script as a service
  2. Write the two files, and then use super user privileges (sudo) to:
    • Move files to the correct directory (sudo mv FileName NewDirectory)
    • Make the files executable (sudo chmod +x Filename)
    • Register the shell script (sudo update-rc.d defaults)

The python script, 

  • Sets up GPIO pin 20 as an input with an internal pull-up resister
  • Waits for GPIO pin 20 to "fall" to ground
  • Execute the shutdown command.

The shell script sets up the python script as a service during start up with two required functions

  • start) Run the python script in the background (a service) waiting for pin 20 to be grounded.
  • stop) "Kill" the python script, service, when the system is shutdown or put to sleep.