314

Should I just pull the plug? Or is it best if I exit LXDE and any other running processes first?

7
  • 60
    Of course I turn off my Pi properly every time... cough
    – berry120
    Commented Jun 18, 2012 at 10:56
  • 11
    sudo halt -p will do it. Commented Aug 1, 2012 at 22:07
  • 13
    i'd love to see a "how-to" for a hard button connected to GPIO to tell the OS to shutdown.
    – user5570
    Commented Jan 23, 2013 at 5:12
  • 27
    People who are just pulling power out from under their Pi's should really stop doing that. I've had two full Rasbian installs start kernel panicking because of corrupt file systems from improper shutdowns (eg. pulling the power). I had to re-image the card in both cases and re-do all the work I put in to configure it.
    – tavis
    Commented Apr 1, 2013 at 22:55
  • 4
    @tavis - When you get a good configuration you're happy with, you really should back it up with something like Win23 Disk Imager. Lightweight, free, etc., will keep an exact copy of the entire Raspberry Pi's OS, so you're only ever a quick trip to Best Buy away from having a fully restored Pi - assuming the issue is disk failure or mis-configuration :-).
    – Gojira
    Commented Nov 7, 2014 at 20:41

15 Answers 15

322

You definitely don't want to just pull the plug. This can cause problems with the SD card and file system.

From the command line

When using the command line or a terminal window, you can enter the following to do a clean shutdown:

sudo shutdown -h now

From the LXDE GUI

  1. Click the shutdown button (red power button) in the menu bar at the bottom right corner of the screen.
  2. Click shutdown (or "logout" as shown on newer versions, then issue the shutdown command listed above).

Once it says System halted, you can remove the power cord if needed.

4
  • 2
    Regarding the LXDE GUI part: Is this still current? I tried with Raspbian corresponding to NOOBS 1.4 released 2015-03, underlying Linux version: Debian 7 (Wheezy) - I am not sure of the exact version, but it is Raspbian ultimo 2014/primo 2015 (the fix for Shellshock bug is included). Pressing the power button in the lower right only offers logout (and cancel). However, there is an icon on the desktop named "Shutdown". I don't know if NOOBS customises/configures Raspbian. Commented May 17, 2015 at 20:05
  • @PeterMortensen it does appear that newer version of Raspbian have removed it from the options so i have made an edit above to address it - thanks. Commented May 17, 2015 at 21:59
  • This is what I do for my headless Zero WH. Still, on three occasions, after running fine for weeks, I do the shutdown, wait for the green LED to go out, pull the plug, move the Pi to another room, and it fails to join the network. I end up pulling the plug on the live Pi and schlep it to my monitor/keyboard area. The system always shows corruption. It's so frustrating. I have to recreate the SD.
    – MACE
    Commented Jun 3, 2020 at 14:32
  • 2
    This means that Raspberry Pi isn't suitable to be plugged to the wall or to run on batteries that may run out of charge? How are we supposed to power the device then? Will it be rendered unusable and corrupt all our data once it loses power such as from a blackout? There are file systems that can handle such situations, so why not use those instead? Commented Jan 31, 2021 at 22:33
67

It's best to always shutdown correctly if you can.

Constantly unplugging the device without shutting down gracefully may have undesireable effects on the SD card (a temperamental object already).

For reference, to shut down from the terminal you can use:

sudo shutdown -h now

or

sudo halt

These commands send SIGTERM to all running processes, notifying them to save their data and exit. It then sends SIGKILL to all remaining processes to halt the system, followed by finally unmounting all filesystems. The screen will then show System Halted and you can pull the plug.

8
  • 8
    sudo poweroff also works just fine.
    – Nilpo
    Commented Jun 24, 2012 at 6:19
  • 5
    Out of interest, what's the difference between halt, shutdown, and poweroff?
    – ACarter
    Commented Jan 7, 2013 at 20:54
  • 2
    @ACarter There's no difference, they all switch the system to runlevel 6.
    – Jivings
    Commented Jan 7, 2013 at 22:46
  • 3
    Its not exactly the same: aplawrence.com/Linux/halt_shutdown_reboot.html
    – Anake
    Commented Jan 11, 2013 at 13:50
  • 2
    @Jivings They're not exactly the same on every single system/distro, for example, some BSD variants will just halt the CPU without even killing any processes when calling halt. Furthermore, you can specify halt to just halt the kernel/CPU right away without taking any processes down; for this reason I would stick to using shutdown unless you have a specific reason not to do so. Commented Jun 24, 2015 at 6:20
31

I've used that shutdown.py solution too.

You'll still need to find a physical switch and solder it to pins 6 and 8 of header 5. I took mine from an old broken Arduino board. I've also drilled a small hole in the case to make sure that I can push the button with a small wooden pin when the case is closed.

This is how it looks like:

Screenshot of the switch arrangement

5
  • As a Raspberry Pi newbie, why is it necessary to find a physical switch and solder it to those pins? Do you need hardware to turn it off properly, or what happens if you just turn it off by using software (i.e. from the shell or from a GUI)? Commented Jun 29, 2019 at 16:55
  • 2
    The point was to do it locally without remote access. There is no button that would gracefully shutdown OS, so just "pulling the plug" as OP has suggested is not a good option.
    – Oleg Gryb
    Commented Jun 29, 2019 at 17:35
  • I’m not sure I follow you. Why is a physical switch soldered to the Pi necessary, or even better than pulling the plug? Commented Jun 30, 2019 at 19:32
  • 1
    No, you're definitely not. I'm not saying that "it's necessary", I'm saying you can use the solution to shutdown the device gracefully without remote access.
    – Oleg Gryb
    Commented Jun 30, 2019 at 19:36
  • Ah, I get it, so it’s only necessary if you use the shotdown.py solution. The file seems to have been moved or removed, though; your link doesn’t point to it anymore. Commented Jul 1, 2019 at 6:18
16

I followed the project described in Raspberry Pi Shutdown Switch – Safely Turning off the Pi, but set pin 17 to be internally pulled up. Then connect the wires like the above project, but leave out any resistors and add the following Python script in /etc/rc.local:

python shutdown_button.py

File shutdown_button.py

import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down = GPIO.PUD_UP)
while True:
    print GPIO.input(17)
    if (GPIO.input(17) == False):
        os.system("sudo shutdown -h now")
        break
    time.sleep(1)
2
  • 1
    A quirk of the above would be that because it checks the state once a second, it will sometimes require holding down the button for a full second, and other times it will appear to start shutting down instantaneously. That's fine, of course, just a quirk.
    – Brian
    Commented Oct 5, 2015 at 20:29
  • 10
    Use GPIO.wait_for_edge(17, GPIO.FALLING) instead of the while loop, is more responsive. Commented Oct 10, 2015 at 23:29
13

Other considerations aside, the power, and other, connectors will be designed for a limited number of insert/disconnect cycles. Often this is a surprisingly low number.

Statistically connectors are a very likely source of equipment failure.

With a view to the overall longevity of your Pi, you might want to moderate the number of connector insert/disconnects it experiences. One way of helping to do this would be to use the mains supply socket's on/off switch.

6
  • So what is the suggestion? Turn the Raspberry Pi off by disconnecting the power supply from the mains (thus reducing the number of insert / remove cycles to the connector on the device)? I find your answer regarding probability of connector failure very interesting. Can you back it up with links to any evidence? Thanks. Commented Jun 20, 2012 at 12:25
  • 4
    @AndrewFogg It's a well know factor in equipment design in Electrical Engineering. In the general case, electronic components do not experience the same degree of mechanical forces as connectors do. Manufacturers mating lifetimes, as you might expect, for connector are obtained in favourable conditions so are often optimistic and do not account for real life human usage.Check data sheets, Edge connectors - 50-100 insertions, RJ45 500-1000 insertions, Micro-USB 5000-10000. So yes, you are correct, turning off at the wall socket is the best choice. Commented Jun 21, 2012 at 7:49
  • Please add the suggestion to your answer. You and I came to the conclusion, but other may not see the implications.
    – Bengt
    Commented Jun 23, 2012 at 21:16
  • Also what about switches? Are they designed for more on/off-circles? Would it be a good idea to place a switch on the AC-side of the power supply?
    – Bengt
    Commented Jun 23, 2012 at 21:18
  • 2
    If it's reachable (and not going into an even more expensive piece of equipment such as a desktop/laptop) I do find it often preferable to plug-cycle the USB "A" end of the cable. Cables are cheap to replace, and the "A" end generally plugs and unplugs more easily without requiring you to firmly grab the board with your fingers. Commented Aug 1, 2012 at 17:51
13

I log into my Raspberry Pi remotely, and here is how I shut it down:

  1. Execute the command:
    sudo shutdown -h now
  2. Wait until the LEDs stop blinking on the Raspberry Pi.
  3. Wait an additional five seconds for good measure (optional).
  4. Switch off the powerstrip that the Raspberry Pi power supply is plugged into.

Since I use a remote display, I don't necessarily see the final output of the Raspberry Pi in the command window, which is why I use the activity lights. The non-blinking state of the LEDs is not an absolute indication of a complete successful shutdown, but it has worked well enough for me.

6

If your Raspberry Pi is frozen then you can not enter to ssh or console, then this helps. I was broke my few SD cards before knew this:

Hold down both Alt+PrintScreen, and while holding those keys, hit the following keys in sequence, one at a time, with a few seconds pause between them.

Alt + PrintScreen + R E I S U O

or

Alt + PrintScreen + R E I S U B (reboot)

A handy mnemonic to remember that is, Reboot Even If System Utterly Broken.

Substitute "O" for "B" to shut down the system instead of rebooting (O=off, B=boot).

Source: https://www.raspberrypi.org/forums/viewtopic.php?t=176612#p1126262

2
3

To avoid the SD card corruption issues you should always shut down the OS properly. You can automate this by using a power controller hardware solution and a script that is triggered through a GPIO port signal, for example (in addition to the Pi Supply mentioned above):

The RemotePi Board in the last link has the added functionality of using IR remotes to power on/off the Raspberry Pi.

3

There is multiple ways to power the pi off! Just pulling the plug without proper shutdown could corrupt your SD card!

To shutdown use sudo shutdown -now or sudo shutdown -time tell shutdown in minutes.

To reboot use sudo reboot to restart and sudo reboot -time in minutes to power back on and or use just sudo reboot to restart now sudo shutdown without the -time will default to one minute to cancel shutdown use sudo shutdown -c

Note: You can also press the raspberry pi button and then click power. Then shutdown or reboot as well which shutdown and restart are both immediate!


Update:

As described in some of the other answers on this post:

sudo halt

Will also work: once there is nothing on the screen just pull the plug!

3
  • "no way to power on without pulling the plug and putting it back" -> Incorrect. The RUN header can be used to do a hard reset of the SoC, causing it to boot (whether or not the OS has been shutdown...): raspberrypi.stackexchange.com/q/29339/5538 On some models these are labelled P6, not RUN.
    – goldilocks
    Commented Oct 13, 2017 at 20:55
  • @goldilocks Thank you I did not know that I removed it from my post Commented Oct 13, 2017 at 20:57
  • 3
    sudo shutdown -now does not work, sudo shutdown now works.
    – Danijel
    Commented Nov 8, 2021 at 16:04
2

This is not very hard. Go into the command prompt, and type sudo halt. Enter your password if needed. Once there is nothing on the screen, pull the plug.

Info from the Raspberry Pi website.

1
  • I would only add that after the screen goes dark I double-check to make sure the green light is also out.
    – SDsolar
    Commented Sep 16, 2016 at 21:10
1

There is also a GPIO board called the Pi Supply that has an on switch, power management and hard off for lock ups. It is also useful if your plug is behind the TV and hard to get to. You still need to shut down normally, but it then powers off the Raspberry Pi.

If you're concerned about wearing the power micro USB port out in normal usage you can get a male-to-female USB adapter and plug your power supply in to that.

1

You should only pull the plug, if the green led is off. The green led flashes, if the machine is writing to the sd card.

0

You don't really need a power switch for your Raspberry Pi. I use a USB hub for the power supply that has on/off switches. So, whenever I want to turn off the Raspberry Pi, I just turn the USB switch off.

1
  • 20
    It is not a good idea to just power of the Pi - YOu could risk data loss or corruption in sudden power downs. Better to always shut down as intended. for example - FreeNAS (based on FreeBSD) is safe to do that because the root is readonly; But, because it is a NAS! data can be lost while writing to your hard drives.. So end of day. Always use implemented shutdown methods!
    – Piotr Kula
    Commented Nov 1, 2012 at 16:43
0

The normal unix commands work with Linux, like so:

init 0 will shut it down so it is safe to pull the plug.

init 6 will reboot.

Unlike unix, there is no real need to do sync; sync; before shutting down. But simply pulling the plug can cause errors, so be sure to use shutdown or init 0 before disconnecting power. If you do happen to interrupt power unexpectantly, use fsck to check for and/or fix any problems that might arise.

There might be a case to do the full sync; sync; sync; sleep 30; halt if you have an external file system that does not unmount cleanly. You'd know if this is a problem by running fsck on external filesystems right after booting. If it is finding problems then you might want to do the whole thing. But I have never seen this happen with my Rpi3.

0

Always use a command to power down and shut off your Raspberry Pi. I use sudo halt like alot of other users. I recommend backing up your SD card I accidentally unplugged mine and corrupted the image somehow. It is easier to shutdown than burn a new image and starting over

Not the answer you're looking for? Browse other questions tagged or ask your own question.