34

Usually with a desktop computer, if I run sudo shutdown -P now, the computer switches off completely and I can switch it back on again with a button. (Likewise, if the computer crashes, I can force a restart by holding down the power button for 5 seconds or so.)

The Raspberry Pi doesn't have a power button. In fact, if I run sudo shutdown -P now, the power led still seems to be on, and in order to start it up again, I seem to need to unplug it and plug it back in again. This seems somewhat analogous to "It is now safe to switch off your computer" in old versions of Windows.

At what point is it safe to pull the plug on the Raspberry Pi? Am I doing something wrong?

Will
  • 363
  • 3
  • 16
George Simms
  • 513
  • 1
  • 5
  • 6
  • 2
    The red led just indicates that the oi is receiving power. The one you want to pay attention to is the green one. If it is flashing that means the oi is accessing the microSD card. When you shut down watch it. It should flash 4-10 times. Once this occurs it is safe to unplug. – TheXed Feb 21 '16 at 19:26
  • 1
    Provided your SD card is formatted in ext4 (or other journalling FS) it is frankly more or less safe to turn it off whenever the pi is just sitting around idle. At least more safe than it was to just turn off a Windows95 computer. (Not that I recommend it for any safety critical applications... but a pi isn't exactly ideal then, anyway.) – leftaroundabout Feb 21 '16 at 19:54
  • 4
    @leftaroundabout, journaling doesn't do much when the underlying medium can't handle a power failure cleanly. I frequently need to re-format my SD cards after a power failure. – Mark Feb 21 '16 at 20:11
  • @Mark: possible. I have a pi that I've often unpowered unsafely without complications, but in fact this one only uses the card only for the boot loader and has everything else on a USB hard drive. – leftaroundabout Feb 21 '16 at 20:16
  • @Mark It also doesn't mean much when in the middle of a data write. You'll still lose your data. It just guaranteed filesystem consistency,. – Bob Feb 22 '16 at 04:02
  • @Bob: That's true but a bit meaningless. Power loss is asynchronous. That means it could equally well have happened a millisecond before the write, when the data was still being computed. The bar for a journaling filesystem is to not make things worse. – MSalters Feb 22 '16 at 09:08

8 Answers8

22

You are not doing anything wrong.

The activity LED should flash 3 or 4 times just before power off. It is then safe to remove the power.

I typically shut-down, go away for a few minutes, and then yank the power cord out.

joan
  • 67,803
  • 5
  • 67
  • 102
19

You do not need to remove power to restart Pi. There are a pair of pads near the SD Card (I think labelled reset possibly run - I can't see on my Pi because they all have switch soldered on the board.) Momentarily short to restart.

Recent Rasbpian have an in-built process for shutdown (handled by systemd-logind)

Add the following to /boot/config.txt

dtoverlay=gpio-shutdown,gpio_pin=5

This enables a switch connected between pin 29 (GPIO 5) and pin 30 (Gnd) to initiate an orderly shutdown of the Pi.

Almost any pin can be used - the default is pin 5 (GPIO 3), although this is often used for I²C ,gpio_pin=21 would use the same pins used in the script pin 40 (GPIO 21) and pin 39 (Gnd)

I recommend sudo poweroff to shut down the Pi. There is nothing wrong with what you are doing, but poweroff causes the green LED to blink 10 times at 1 second intervals when it is safe to poweroff.


Just to be clear - the following script was the original Answer. While it should work it is no longer necessary.

I have a Python script which shuts the Pi down with a pushbutton.

#!/usr/bin/env python2.7
#-------------------------------------------------------------------------------
# Name:         Shutdown Daemon
#
# Purpose:      This program gets activated at the end of the boot process by
#               cron. (@ reboot sudo python /home/pi/shutdown_daemon.py)
#               It monitors a button press. If the user presses the button, we
#               Halt the Pi, by executing the poweroff command.
#
#               The power to the Pi will then be cut when the Pi has reached the
#               poweroff state (Halt).
#               To activate a gpio pin with the poweroff state, the
#               /boot/config.txt file needs to have :
#               dtoverlay=gpio-poweroff,gpiopin=27
#
# Author:      Paul Versteeg
#
# Created:     15-06-2015, revised on 18-12-2015
# Copyright:   (c) Paul 2015
# https://www.raspberrypi.org/forums/viewtopic.php?p=864409#p864409
#-------------------------------------------------------------------------------

import RPi.GPIO as GPIO
import subprocess
import time

GPIO.setmode(GPIO.BCM) # use GPIO numbering
GPIO.setwarnings(False)

# I use the following two GPIO pins because they are next to each other,
# and I can use a two pin header to connect the switch logic to the Pi.
# INT = 17    # GPIO-17 button interrupt to shutdown procedure
# KILL = 27   # GPIO-27 /KILL : this pin is programmed in /boot/config.txt and cannot be used by any other program
INT = 21    # GPIO button interrupt to shutdown procedure

# use a weak pull_up to create a high
GPIO.setup(INT, GPIO.IN, pull_up_down=GPIO.PUD_UP)

def main():

    while True:
        # set an interrupt on a falling edge and wait for it to happen
        GPIO.wait_for_edge(INT, GPIO.FALLING)
#       print "button pressed"
        time.sleep(1)   # Wait 1 second to check for spurious input
        if( GPIO.input(INT) == 0 ) :
            subprocess.call(['poweroff'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

if __name__ == '__main__':
    main()
Milliways
  • 54,718
  • 26
  • 92
  • 182
  • Can you add that python code for reference, and what if I give the power source will it automatically start pi or else? – Gaurav Dave Feb 25 '16 at 12:59
4

No one seems to have answered this question: "At what point is it safe to pull the plug on the Raspberry Pi?"

To properly shutdown, you run

sudo shutdown

When you do this, you'll see the "ACT" light (the green one) blink solidly 10 times (0.5 second intervals). Once it stops blinking, the green light will turn off. At this point, it is safe to remove the power or pull the plug.

The red light will remain on as long as there is power applied to the Pi.

After shutdown, you must remove the power and then apply the power again to power up the Pi.

4

Here is a really simple shutdown python script.

import RPi.GPIO as GPIO 
import os 
channel=11 
GPIO.setmode(GPIO.BOARD) 
#Pin 11 & Gnd 

GPIO.setup(channel, GPIO.IN, pull_up_down=GPIO.PUD_UP) 
GPIO.wait_for_edge(channel, GPIO.FALLING) 
os.system("sudo shutdown -h now")
Andy Anderson
  • 608
  • 4
  • 11
2

As previous answers have stated, the red led means the pi is receiving power where as the green led is activity (I believe disk activity)

you can either pull the plug when the green led has stopped blinking after a shutdown command or you can short the run/reset pads that are on the board

This website has a great instructions on how to add a hard reset switch if your willing to solder a few pins.

If you do use the hard reset be sure to only use it after a halt or system shutdown or as a last resort as it immediately restarts the processor, if you are writing to your SD card then you can potentially corrupt it just as pulling the power while it running

S.Rose
  • 61
  • 1
  • 1
  • 6
  • It is good to think of the green light as an indicator, one of the things it may be indicating being active writes to the sdcard. Certain repeating flash sequences may indicate other things. It is not a bad idea to consider what seems a somewhat random (or constant) flashing as sdcard writes, and for overall awareness, also look into what else it might mean in the documentation. A minute after a proper shutdown command, if it hasn't flashed for quite some time, (and an attached monitor, if any, has had no signal for 10 seconds), that is when I switch off the surge protector or pull the plug. – always_learning Apr 02 '20 at 17:33
1

If you're using a graphical desktop, you can select "logout" and then "shutdown". Different DEs (Gnome, KDE, XFCE) provide similar options in their menus. If you're using a WM (window manager) like Openbox, I3, Awesome, etc, just "logout" (kill your current x session) and select the "shutdown" option on your display manager. If you're not using a display manager and you've started your x session from the command line (console), or if you're not running a graphical environment, from the Linux console (tty) simply run "sudo halt -h" ("poweroff" and "systemctl poweroff" will also work!).

THEN wait until all the LEDs except the power LED are off.

THEN wait an additional second to make sure the SD card can finish its wear-levelling tasks and write actions.

You can now safely unplug the Raspberry Pi. Failure to shut the Raspberry Pi down properly may corrupt your SD card, which would mean you would have to re-image it.

You can find this answer on the official documentation :) for the raspberry pi!

mariano
  • 11
  • 2
0

See my automated Raspi-Shutdown Solution "RasPi On/Off" here:
https://github.com/nlohr1/Raspi-On-Off
It works either with a knob (On/Off) or a I/O-line (5V/0V) or scanning the mains-Power-Line
(f.ex. as automatic On/Off Steering-Box for a 3D-Printer).

-1

Try using the command "sudo halt" It always works for me.

UNKNOWN
  • 79
  • 3
  • 18