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
  • 371
  • 3
  • 16
George Simms
  • 513
  • 1
  • 5
  • 6

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
  • 71,852
  • 5
  • 76
  • 108
20

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
  • 62,573
  • 32
  • 113
  • 225
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.

PFAS Expert
  • 314
  • 2
  • 6
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
  • 649
  • 1
  • 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
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
  • 99
  • 3
  • 6
  • 23