How do I turn off my Raspberry Pi?
Should I just pull the plug? Or is it best if I exit LXDE and any other running processes first?
i'd love to see a "how-to" for a hard button connected to GPIO to tell the OS to shutdown.
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 - 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 :-).
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
- Click the shutdown button (red power button) in the menu bar at the bottom right corner of the screen.
- 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.
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.
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
These commands send
SIGTERMto all running processes, notifying them to save their data and exit. It then sends
SIGKILLto all remaining processes to halt the system, followed by finally unmounting all filesystems. The screen will then show
System Haltedand you can pull the plug.
Out of interest, what's the difference between `halt`, `shutdown`, and `poweroff`?
Its not exactly the same: http://aplawrence.com/Linux/halt_shutdown_reboot.html
@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.
@Breakthrough This will be largely irrelevant when everything moves to `systemd`, where all shutdown commands symlink to the same thing.
Don’t forget that you can also HCF (half and catch fire) https://en.m.wikipedia.org/wiki/Halt_and_Catch_Fire
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:
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)?
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.
I’m not sure I follow you. Why is a physical switch soldered to the Pi necessary, or even better than pulling the plug?
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.
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
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)
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.
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.
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.
@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.
Please add the suggestion to your answer. You and I came to the conclusion, but other may not see the implications.
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?
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.
After all, it seems to be a sensible choice to switch the power supply off on the AC side because of security and energy consumption considerations. If it's the typical wall-plug type of power supply, a simple in-line socket with a switch (ideally 2-pin, depending on your type of AC plug) would do.
I log into my Raspberry Pi remotely, and here is how I shut it down:
- Execute the command:
sudo shutdown -h now
- Wait until the LEDs stop blinking on the Raspberry Pi.
- Wait an additional five seconds for good measure (optional).
- 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.
- Execute the command:
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
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).
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.
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.
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 -nowor
sudo shutdown -timetell shutdown in minutes.
To reboot use
sudo rebootto restart and
sudo reboot -time in minutesto power back on and or use just
sudo rebootto restart now
sudo shutdownwithout the
-timewill 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!
As described in some of the other answers on this post:
Will also work: once there is nothing on the screen just pull the plug!
*"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...): https://raspberrypi.stackexchange.com/q/29339/5538 On some models these are labelled P6, not RUN.