When you’re running your Raspberry Pi headless, one of the first things you should concern yourself with is how to shut it down safely. Shutting it down by simply pulling the power out can result in a corrupt SD card or damaged files. If you had it connected to a network, you could of course just SSH into it and issue the ‘halt’ command. However, what if you couldn’t connect to it? Or what if you just wanted a way to simply and easily shut it down without touching a keyboard at all?
Here’s where Adafruit steps in and gives us a simple script that will monitor a GPIO pin and shut down if it detects a change in state. What use is that? Well, it means you just simply need to connect two pins on the GPIO together with a paper clip (or anything conductive) to issue the halt command. I’ve taken the Adafruit tutorial and some other guides online and put together the following instructions.
First of all, you will need a Pi with Git installed on it.
sudo apt-get install git
Next, you download the script from Adafruit’s Github:
git clone https://github.com/adafruit/Adafruit-GPIO-Halt
If this doesn’t work, try:
git clone git://github.com/adafruit/Adafruit-GPIO-Halt
An aside: The script is written in C, so is able to be modified if you want to, say, light up an LED when the halt command is issued. You could re-write it as a Python program, of course, which might make it easier for some to adapt.
Change into the new directory, compile and install:
cd Adafruit-GPIO-Halt make sudo make install
This installs the script to /usr/local/bin/gpio-halt. You then need to run it as a service.
In the following sections, if you’re using an older Pi with a 26-pin GPIO header, use GPIO pin 7 instead of 21.
Run it automatically on Jessie/Buster
If you’re running Raspbian Jessie, you will need to do it via systemd:
sudo nano /lib/systemd/system/gpio-halt.service
This will create a file and open it. The following is the contents of that file:
[Unit] Description=Short pins 21 and ground to shutdown the Pi After=multi-user.target [Service] Type=idle ExecStart=/usr/local/bin/gpio-halt 21 & [Install] WantedBy=multi-user.target
Then, make the script executable by the right users:
sudo chmod 644 /lib/systemd/system/gpio-halt.service
Then, tell systemd to use the script:
sudo systemctl daemon-reload sudo systemctl enable gpio-halt.service
And reboot your Pi
When the Pi comes back up, you can check the status of the service by doing:
sudo systemctl status gpio-halt.service
Run it automatically on Wheezy
If you’re running the older Raspbian Wheezy, you will need to do it via rc.local:
sudo nano /etc/rc.local
Before ‘exit 0’, add the line:
/usr/local/bin/gpio-halt 21 &
Now reboot your Pi:
Now use it
Take a paper clip (or other conductive object) and touch the last two vertical GPIO pins at the same time. If you’re using a 26-pin Pi, it will be GND and GPIO7. If you’re using a 40-pin Pi, it will be GND and GPIO21. See the diagram below. The first time you do this, it might be worth having a monitor connected so that you can see it happening. Wait about 15 seconds for the halt procedure to complete and then unplug your Pi safely.