Should I just pull the plug? Or is it best if I exit LXDE and any other running processes first?
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.
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
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.
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:
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)
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.
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.
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.
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!
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!
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.
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.