I’ve been using the raspberry pi for some time and since I use it as s headless device, I have missed the shutdown button. After some experimentation, I have come up with a simple solution. There are many ways to implement a shutdown script, but I am going to walk through the two simplest ones.
We need a physical button which we can press to shutdown the RPi and I really did not want to waste the GPIO Connector P6 that we typically use.
Shown above is the connector which I am talking about. While going through the RPi documentation, I found another GPIO connector which can be used.
So naturally I soldered a connector to it and additionally I soldered a simple push button to a relimate connector. The result is shown below.
Its that simple!. Now we have the physical button. No additional hardware is required! Beauty!
There are two parts to the software..
- The script that will execute the shutdown and
- Adding the script to be executed automatically when the system is started.
The Python Way
We can use a simple python script to shutdown the RPi. This script simply waits for the button to be pressed and when that happens, a command is sent to the kernel to safely shutdown the RPi. The code is as follows;
# Simple script for shutting down the raspberry Pi at the press of a button. # by Inderpreet Singh import RPi.GPIO as GPIO import time import os # Use the Broadcom SOC Pin numbers # Setup the Pin with Internal pullups enabled and PIN in reading mode. GPIO.setmode(GPIO.BCM) GPIO.setup(31, GPIO.IN, pull_up_down = GPIO.PUD_UP) # Our function on what to do when the button is pressed def Shutdown(channel): os.system("sudo shutdown -h now") # Add our function to execute when the button pressed event happens GPIO.add_event_detect(31, GPIO.FALLING, callback = Shutdown, bouncetime = 2000) # Now wait! while 1: time.sleep(1)
Copy the script code and create a file using nano and save it in a known location as shutdown.py. I created a folder in my home folder called scripts and saved them there. In order to test the script run the following command
sudo python /home/pi/scripts/shutdown.py
Pressing the button should shutdown the RPi
Bourne Again Shell
The working of the bash script is quite simple and the comments explain the working of the script. It is basically monitoring the button for a press. I have not done any load testing to see which one will load the cpu more and if there is anyone who can suggest testing results I would be more than happy to append it here.
#!/bin/bash # monitor GPIO pin 31 (wiringPi pin 1) for shutdown signal # export GPIO pin 31 and set to input with pull-up echo "31" > /sys/class/gpio/export echo "in" > /sys/class/gpio/gpio31/direction echo "high" > /sys/class/gpio/gpio31/direction # wait for pin to go low while [ true ] do if [ "$(cat /sys/class/gpio/gpio31/value)" == '0' ] then echo "Raspberry Pi Shutting Down!" halt & exit 0 fi sleep 1 done
Copy the script code and create a file using nano and save it in a known location. I created a folder in my home folder called scripts and saved them there.
Test the bash script by running the following commands:
sudo chmod 755 /home/pi/scripts/shutdown.sh sudo sh /home/pi/scripts/shutdown.sh
Press the button and the system should shutdown.
Starting the script at startup
Now that we have the scripts we need to set up the RPi to run them at startup. This is as simple as adding the commands to the /etc/rc.local file. Be sure to add an & so that the script runs in the background and does not wait there forever.
sudo nano /etc/rc.local
It should look like the above screenshot. In case you opt for the python option, simply replace the corresponding command.
I have tried to explain the simple process as much as possible. Please leave a comment below for queries etc.