Safe Power Down
Setting up an OS service to shutdown the Raspberry Pi requires two short scripts and registering the shell to run at startup:
- A short python script to
- Set up a GPIO pin to wait to be pulled to be grounded
- When grounded, run the shutdown command
- A shell script to set the python script as a service
- 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 listen_for_shutdown.sh defaults)
- 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.
- 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.