Comments: Raspberry Pi Safe Reboot and Shutdown Button

Pages

Looking for answers to technical questions?

We welcome your comments and suggestions below. However, if you are looking for solutions to technical questions please see our Technical Assistance page.

  • Error Booting Up

    If you have a typo in the python script or rc.local file, your Raspberry Pi might have problems booting up into the desktop GUI. If you notice this happening, try going into safe mode and editing the rc.local file through the command line as indicated by the instructions below.

    • Insert the microSD card into another computer.
    • Open the cmdline.txt file (this will appear as a removeable drive under /boot).
    • Add "init=/bin/bash" at the end of the file with a space between each kernel parameter.
    • Save the file.
    • Eject the microSD card from your computer
    • Insert it back into the Raspberry Pi.
    • Add power to the Raspberry Pi.
    • Execute the following command to edit and write changes to files: mount -o remount,rw /dev/mmcblk0p2 /
    • Open the rc.local file by typing the command: "sudo nano /etc/rc.local"
    • Assuming the rc.local file was edited incorrectly, undo the changes by removing the following line to set the Pi back to its previous state: "python /home/pi/safe_restart_shutdown_Pi.py &"
    • Save the file.
    • Eject the microSD card from the Pi.
    • Insert the microSD card in to a computer.
    • Reopen the cmdline.txt file.
    • Remove the kernel parameter "init=/bin/bash".
    • Save the file.
    • Eject the microSD card from your computer.
    • Insert the microSD card back into your Pi.

    Powering the Raspberry Pi back up to get back to the desktop GUI. Try checking your Python script to make sure that there are no errors. When ready, try adding the command back into the rc.local file again to execute the Python script. You will need to ensure that the command has no syntax errors. Otherwise, the Raspberry Pi will encounter the same problem booting up again.

    Resources and Going Further

    For more information about getting your Raspberry Pi into safe mode, check out the following post:

  • Member #1663596 / about 2 years ago / 1

    Due to the polling, top is reporting ~92 percent cpu usage when I apply this on my Raspberry Pi 1 Model B. Is there a way to do this using interrupts so that the code doesn't have to poll every half second?

    • Sweeeeeet, I got the interrupt to also to work for the safe reboot and shutdown example. I'll need to test both out with the rc.local and observe the performance. It's looking pretty good though since the interrupts are waiting for falling edge. Feel free to look at my gists if you don't want to wait for me to update this tutorial with the code commented, cleaned, and tested.

    • Hi,

      There was another user that was wondering the same thing. At the time of writing, I didn't realize that the example code used a lot of resources when using it with a Pi 3. We use part of that code in production to test, program, and check certain boards. I don't believe we ever tested its performance by viewing it with the top command.

      Possible Solutions

      I am not sure which example you are referring to but both examples are probably using a lot of resources. When the case was brought up from another user, I checked in with a co-worker and he believed that it might have something to do with the while loop. He suggested possibly using an interrupt as opposed to the while loop to check the button state. The other option that I was thinking is possibly adding a delay so that the piece of code is not constantly checking and running so much. The restart code already uses a delay to check how long it has been pressed so it may need to be placed as an additional else statement when it is not being pressed down.

      Initial Tests with Interrupts

      Looking at the documentation that is linked for the RPI.GPIO from PYPI, it appears to be efficient when using the wait_for_edge() example. I briefly tested using the wait_for_edge() in the shutdown example in Thonny IDE. I'll need to configure the rc.local to run the example at startup to observe the performance but I think it will perform better than the original code. The next step is revisiting the safe reboot and shutdown example with the interrupt as well to see if that code can be improved as well. =)

  • It may be possible to recover a corrupt microSD card if your Raspberry Pi is stuck in emergency mode. This happens when you remove power from your Pi without properly shutting it down. This happened to me once but I never got it working (I simply flashed a new image on another microSD card).

    Here are the associated links that may be of some use if anyone runs into this issue:

  • Curtis / about 3 years ago / 1

    I have a question with the scripts as presented. Why enable the internal pull-up resistor in the Pi for the presented application when the button on the Qwiic Phat already has one?

    • Hi,

      That is a good question. I assumed that there was not a pull-up resistor on the board to prevent the pin from floating similar a few of my other projects. When testing, I did not notice any problems resetting or shutting down. I assumed that it was correct when checking against code that we use. However, that is redundant now that I look at the schematic.

      Let me test the case out tonight when I have the Pi available. The following line can probably be adjusted from:

      # Use built-in internal pullup resistor so the pin is not floating
      GPIO.setup(reset_shutdown_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
      

      to

      # Use Qwiic pHAT's pullup resistor so that the pin is not floating
      GPIO.setup(reset_shutdown_pin, GPIO.IN)
      

      • Curtis / about 3 years ago / 1

        I would recommend leaving both options in the code and change your write up to state enable the GPIO pull-up line if duplicating the project without using the Qwiic Phat (just a push-button connected between GND and GPIO17) or use the other option if using the Phat.

        • I tested this out and my Pi seems to be running fine with the internal pull-up resistor commented out since Friday night. The code in example 1 and 2 have been updated so that the options are available. I also included a note in the tutorial. =)

          Thanks for the feedback. Stay healthy and safe. ^_^


If you've found a bug or have other constructive feedback for our tutorial authors, please send us your feedback!