# How to Make a Safe Shutdown Button for Raspberry Pi

**URL:** <https://forum.core-electronics.com.au/t/how-to-make-a-safe-shutdown-button-for-raspberry-pi/3129>\
**Category:** Guides\
**Created:** [20 July 2018 22:14 UTC](https://forum.core-electronics.com.au/t/how-to-make-a-safe-shutdown-button-for-raspberry-pi/3129 "2018-07-20T22:14:54Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Clinton](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/clinton/32/1515_2.png) [@Clinton](https://forum.core-electronics.com.au/u/Clinton)\
**Post date:** [20 July 2018 22:14 UTC](https://forum.core-electronics.com.au/t/how-to-make-a-safe-shutdown-button-for-raspberry-pi/3129/1 "2018-07-20T22:14:54Z")

</div>

Clinton just shared a new tutorial: [**"How to Make a Safe Shutdown Button for Raspberry Pi"**](http://core-electronics.com.au/tutorials/how-to-make-a-safe-shutdown-button-for-raspberry-pi.html)  
  
 ![](https://canada1.discourse-cdn.com/flex028/uploads/core_electronics/original/2X/d/df18e9b5fb43ac0c2d3b716db6a5c94de2e092c3.jpg)  
  
Pulling the power to your Raspberry Pi can cause image corruptions & other issues that can damage your Pi! In this article, we will create a small push button that will work as a ‘Safe-Shutdown’ option. We’ll connect it to our R…  
  
[**Read more**](http://core-electronics.com.au/tutorials/how-to-make-a-safe-shutdown-button-for-raspberry-pi.html)

---

<div class="post-metadata">

**Author:** ![Shane53869](https://avatars.discourse-cdn.com/v4/letter/s/bb73d2/32.png) [@Shane53869](https://forum.core-electronics.com.au/u/Shane53869)\
**Post date:** [24 July 2018 23:44 UTC](https://forum.core-electronics.com.au/t/how-to-make-a-safe-shutdown-button-for-raspberry-pi/3129/2 "2018-07-24T23:44:58Z")

</div>

Hey @Clinton

This is an awesome idea, especially since I’ve been thiking for sometime about getting one of these bad boys - [NesPi Case](https://core-electronics.com.au/retroflag-nespi-case-for-raspberry-pi.html) for my RetroPie.

Quick (and probably dumb) question, will this also work to turn the RasPi back on again if it powered down???

Thanks,  
Shane

---

<div class="post-metadata">

**Author:** ![Clinton](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/clinton/32/1515_2.png) [@Clinton](https://forum.core-electronics.com.au/u/Clinton)\
**Post date:** [25 July 2018 01:34 UTC](https://forum.core-electronics.com.au/t/how-to-make-a-safe-shutdown-button-for-raspberry-pi/3129/3 "2018-07-25T01:34:37Z")

</div>

Hey Shane,  
No this won’t but if you wire the button to GPIO3 and add  
`dtoverlay=gpio-shutdown`  
to /boot/config.txt it will give you a safe shutdown and start up again.

You can find out more about it in the [overlay readme](https://github.com/raspberrypi/firmware/blob/master/boot/overlays/README) search for shutdown and you will find it.  
@Josh Told me about this just after I finished writing the tutorial.

---

<div class="post-metadata">

**Author:** ![Shane53869](https://avatars.discourse-cdn.com/v4/letter/s/bb73d2/32.png) [@Shane53869](https://forum.core-electronics.com.au/u/Shane53869)\
**Post date:** [25 July 2018 01:50 UTC](https://forum.core-electronics.com.au/t/how-to-make-a-safe-shutdown-button-for-raspberry-pi/3129/4 "2018-07-25T01:50:55Z")

</div>

Hey Clinton,

Thanks for replying so soon.

That all sounds like a great overall solution for the whole safe shutdown / power / power off questions in my head.

That was about my only reservation with purchasing the NesPi case. I think now you’ve talked me into it.

---

<div class="post-metadata">

**Author:** ![Richard83832](https://avatars.discourse-cdn.com/v4/letter/r/22d042/32.png) [@Richard83832](https://forum.core-electronics.com.au/u/Richard83832)\
**Post date:** [14 May 2019 00:58 UTC](https://forum.core-electronics.com.au/t/how-to-make-a-safe-shutdown-button-for-raspberry-pi/3129/5 "2019-05-14T00:58:26Z")

</div>

I am running an OpenCV tracker on my Pi, and now want to operate it with just a switch and the minimum number of buttons as follows:  
Toggle switch to power on the Pi  
Press button 1 to run (or re-run) OpenCV tracker program  
Press button 2 to shut down safely (per your tutorial)

How would I edit your safe shutdown script to allow for another button to also run my tracker from startup?

---

<div class="post-metadata">

**Author:** ![Stephen](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/stephen/32/1599_2.png) [@Stephen](https://forum.core-electronics.com.au/u/Stephen)\
**Post date:** [15 May 2019 02:50 UTC](https://forum.core-electronics.com.au/t/how-to-make-a-safe-shutdown-button-for-raspberry-pi/3129/6 "2019-05-15T02:50:54Z")

</div>

Hi Richard,

Check out these discussions about how to start a program from a pushbutton:  
[https://www.raspberrypi.org/forums/viewtopic.php?f=32&t=100722](https://www.raspberrypi.org/forums/viewtopic.php?f=32&t=100722)  
[https://www.raspberrypi.org/forums/viewtopic.php?f=32&t=96424](https://www.raspberrypi.org/forums/viewtopic.php?f=32&t=96424)

A toggle switch to power the Pi would require no code, just a physical switch inline with the power:  
[https://core-electronics.com.au/micro-usb-in-line-power-switch-cable.html](https://core-electronics.com.au/micro-usb-in-line-power-switch-cable.html)

See chapter 3.5 of the Raspberry Pi Tutorial for information on starting a program from startup.  
[https://core-electronics.com.au/tutorials/raspberry-pi-workshop-for-beginners.html](https://core-electronics.com.au/tutorials/raspberry-pi-workshop-for-beginners.html)

---

<div class="post-metadata">

**Author:** ![Clinton](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/clinton/32/1515_2.png) [@Clinton](https://forum.core-electronics.com.au/u/Clinton)\
**Post date:** [16 May 2019 00:41 UTC](https://forum.core-electronics.com.au/t/how-to-make-a-safe-shutdown-button-for-raspberry-pi/3129/7 "2019-05-16T00:41:55Z")

</div>

Hi Richard

You can define more buttons by assigning them to free GPIO pins with this command  
`myButton = Button(8) # defines the button as an object and chooses GPIO 8`  
and anything you place inside the if statement will run, the os.system will run any terminal command you put in it.

---

<div class="post-metadata">

**Author:** ![Richard83832](https://avatars.discourse-cdn.com/v4/letter/r/22d042/32.png) [@Richard83832](https://forum.core-electronics.com.au/u/Richard83832)\
**Post date:** [16 May 2019 05:15 UTC](https://forum.core-electronics.com.au/t/how-to-make-a-safe-shutdown-button-for-raspberry-pi/3129/8 "2019-05-16T05:15:29Z")

</div>

Hi Clinton, thanks for your interest in my project. I had tried adding another button to your tutorial script as follows:  
Above the line stopButton = Button(26) I inserted runButton = Button(21)

And then immediately under the line while True: I inserted

```
if runButton.is_pressed: 

    time.sleep(1) 

    if runButton.is_pressed: 

        os.system("python3 /home/pi/picam.py")

```

When I ran this edited button script with a terminal command, the runButton runs my Picam tracking script at the first press only, but won’t re-run the tracking script again at a second press (if say the tracker looses its target). I have to use the shutdown button and then reboot the Pi to re-run the tracking script. Do you think I could improve either the button script or tracking script to be able to re-start the tracking script without re-booting the Pi or adding another button? I am hoping to run this improved button script at startup using rc.local so my tracker bot can be set free.

---

<div class="post-metadata">

**Author:** ![Clinton](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/clinton/32/1515_2.png) [@Clinton](https://forum.core-electronics.com.au/u/Clinton)\
**Post date:** [16 May 2019 05:53 UTC](https://forum.core-electronics.com.au/t/how-to-make-a-safe-shutdown-button-for-raspberry-pi/3129/9 "2019-05-16T05:53:25Z")

</div>

My guess is that the Python script is waiting for the Terminal operation to finish running before continuing.  
Try running it with an “&” character at the end of the line to suppress the output and it may work,  
`os.system("python3 /home/pi/picam.py&")`

---

<div class="post-metadata">

**Author:** ![Richard83832](https://avatars.discourse-cdn.com/v4/letter/r/22d042/32.png) [@Richard83832](https://forum.core-electronics.com.au/u/Richard83832)\
**Post date:** [17 May 2019 00:59 UTC](https://forum.core-electronics.com.au/t/how-to-make-a-safe-shutdown-button-for-raspberry-pi/3129/10 "2019-05-17T00:59:59Z")

</div>

Thanks Clinton, I tried your “&” suggestion, which caused my Pi to freeze, and a black screen on reboot. Maybe I made a mistake with execution, but I think I should reinstall my card, hopefully find another solution, such as a third button to stop the tracker script so it can be re-started when it loses the target.

---

<div class="post-metadata">

**Author:** ![Clinton](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/clinton/32/1515_2.png) [@Clinton](https://forum.core-electronics.com.au/u/Clinton)\
**Post date:** [17 May 2019 02:49 UTC](https://forum.core-electronics.com.au/t/how-to-make-a-safe-shutdown-button-for-raspberry-pi/3129/11 "2019-05-17T02:49:20Z")

</div>

Sorry about that, I forgot that Playing with that script can be dangerous.  
Another thing that might help is CRON it can be setup to run a process every few minutes.

> **[A Beginners Guide To Cron Jobs - OSTechNix](https://www.ostechnix.com/a-beginners-guide-to-cron-jobs/)**
>
> This guide explains the basic usage of Cron Jobs in Linux and two websites that helps you to easily generate a crontab expression.

---

<div class="post-metadata">

**Author:** ![Stephen](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/stephen/32/1599_2.png) [@Stephen](https://forum.core-electronics.com.au/u/Stephen)\
**Post date:** [17 May 2019 03:22 UTC](https://forum.core-electronics.com.au/t/how-to-make-a-safe-shutdown-button-for-raspberry-pi/3129/12 "2019-05-17T03:22:26Z")

</div>

Hi Richard,

It could be that your Pi is unable to run multiple instances of that program at once. Perhaps include a bit in your script that closes any already running instances before starting a new one.

---

<div class="post-metadata">

**Author:** ![Richard83832](https://avatars.discourse-cdn.com/v4/letter/r/22d042/32.png) [@Richard83832](https://forum.core-electronics.com.au/u/Richard83832)\
**Post date:** [1 August 2019 02:25 UTC](https://forum.core-electronics.com.au/t/how-to-make-a-safe-shutdown-button-for-raspberry-pi/3129/13 "2019-08-01T02:25:06Z")

</div>

I am using the script from your tutorial “How to Make a Safe Shutdown Button for Raspberry Pi”. My project is powered by 2 Lipo 18650 batteries via an Adafruit PowerBoost 1000C regulator, and I’d like an automatic shut down when the low battery LED flashes.  
The Adafruit regulator specs state: " **LBO** - not a leveraged buy out! this is the **Low**  **Battery Output**. By default it is pulled high to **BAT** but when the charger detects a low voltage (under 3.2V) the pin will drop down to 0V. You can use this to signal when its time to shut down or alert the user that the battery is low. There is also a red LED connected to this pin." Could you please explain how to use the output from LBO pin to shut down my Pi, and whether this output could be connected to the same GPIO 26 when pressing the stopButton.

---

<div class="post-metadata">

**Author:** ![Robin57159](https://avatars.discourse-cdn.com/v4/letter/r/cc9497/32.png) [@Robin57159](https://forum.core-electronics.com.au/u/Robin57159)\
**Post date:** [2 August 2019 01:29 UTC](https://forum.core-electronics.com.au/t/how-to-make-a-safe-shutdown-button-for-raspberry-pi/3129/14 "2019-08-02T01:29:30Z")

</div>

@Clinton didn’t put a space (required) before the “&” so the process wouldn’t have gone into the background. Not that it matters as os.system waits for the process to finish.

Instead of os.system, use subprocess

```auto
import subprocess

subprocess.Popen("nohup python3 /home/pi/picam.py >&/dev/null &", shell=True)

```

This doesn’t wait for subprocess to finish before continuing.

“nohup” prevents the subprocess dying when the parent process ends, or the controlling terminal closes.

The \>&/dev/null redirects stdout and stderr to the bin basically. If you want to capture the output, substitute the name of your log file.

The final & puts the subprocess into background.

---

<div class="post-metadata">

**Author:** ![Adrian94073](https://avatars.discourse-cdn.com/v4/letter/a/5daacb/32.png) [@Adrian94073](https://forum.core-electronics.com.au/u/Adrian94073)\
**Post date:** [11 August 2019 04:04 UTC](https://forum.core-electronics.com.au/t/how-to-make-a-safe-shutdown-button-for-raspberry-pi/3129/15 "2019-08-11T04:04:55Z")

</div>

@Clinton, I noticed at the top you mention you could add the button at GPIO 3 and then add `dtoverlay=gpio-shutdown`to /boot/config.txt it will give you a safe shutdown and start up again. My question is, do I need to remove the other script I added from the tutorial and then only make this change to config.txt?  
Thanks for your help.

---

<div class="post-metadata">

**Author:** ![Clinton](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/clinton/32/1515_2.png) [@Clinton](https://forum.core-electronics.com.au/u/Clinton)\
**Post date:** [5 September 2019 05:54 UTC](https://forum.core-electronics.com.au/t/how-to-make-a-safe-shutdown-button-for-raspberry-pi/3129/16 "2019-09-05T05:54:25Z")

</div>

The script from the tutorial will be a bit redundant with the pin three activated so it is probably best to remove it.

---

<div class="post-metadata">

**Author:** ![Adam16385](https://avatars.discourse-cdn.com/v4/letter/a/8baadc/32.png) [@Adam16385](https://forum.core-electronics.com.au/u/Adam16385)\
**Post date:** [30 June 2020 04:12 UTC](https://forum.core-electronics.com.au/t/how-to-make-a-safe-shutdown-button-for-raspberry-pi/3129/17 "2020-06-30T04:12:36Z")

</div>

I realise this is super old now, but wanted to note for anyone coming along to use the code: on a Pi 4 I had to fix the first line as so:

`from gpiozero import Button #import button from the Pi GPIO library`

and also add `# -*- coding: utf-8 -*-` at the top of the code to get it to work. YMMV.
