Jun 232012
 

How to safely shutdown or reboot your raspberry pi (Debian/Raspbian)

(If you came here because your B+ won’t fully shutdown, visit this page.)

Of course, we all know you can shut down any computer by pulling the plug or switching off the switch. Most PCs can be forcibly switched off – even when they’ve crashed – by holding the power button down for 3 seconds. But we also know, or should know, that doing this isn’t good for the computer. For desktops and laptops, it can cause problems on the hard disk drive. For the Raspberry Pi, it can cause problems on your SD card. In fact it can corrupt your SD card so that you have to re-image it. Not a huge deal, unless you’ve just done a long compile and not yet backed up the files. :cry:

In the short month I’ve been ‘Pi’ing, I’ve only had one card need re-imaging (running an early version of OpenELEC). I don’t think it was down to an “unofficial hard reboot”* though because I don’t generally do them. I found out very early on how to shut down the Pi properly. You can do this, even when it’s crashed or frozen, as long as you have…

  1. ssh enabled, and
  2. access to another computer on the same network (you can even use your smartphone.)

But before we go into that, let’s talk about how to shut down the proper way when your system isn’t frozen.

So. What is the best way to shut down before you power off the Raspberry Pi?

sudo shutdown -h now (or sudo halt)

You can’t use shutdown (or halt) unless you have sudo privileges.

-h means halt the system
now means do it straight away. You could also add number 10 to tell it to shut down in 10 minutes. You can even give a specific time 19:45 (in 24 hour format with a : colon).

sudo shutdown -h now

Notice that when you give the shutdown command it sends out a message to all logged in users
“The system is going down for system halt NOW!”

If you are working directly on the Pi, within a few seconds you should get a message that the system has halted. If you are logged in through ssh, you’ll find that your console window will close.

If you merely want to reboot

To reboot, you can change the -h for -r like this…

sudo shutdown -r now (or sudo reboot)

sudo shutdown -r now

This time the message is…
“The system is going down for reboot NOW!”

So what if the system’s frozen?

That’s when ssh comes in handy. Use an ssh client program on another computer connected to the same network. Start an Xterm console window and use the above commands…

sudo shutdown -h now (or sudo halt) OR
sudo shutdown -r now (or sudo reboot)

…but be aware that when the system goes down or reboots, your ssh session will end (obviously, you can’t be connected to a machine which is shut down).

Good ssh clients are tunnelier and Putty (Open Source – Windows/Linux)

I may go into how to use ssh in another post when I get time :-D

________
* “unofficial hard reboot” = pulling the plug

  30 Responses to “How to safely shutdown or reboot your raspberry pi”

  1. You can also use the shorthands `halt` and `reboot`.

  2. Thanks Stephan. :-)

    I knew the reboot one, but hadn’t come across halt before.

    For those who want to try these – they need sudo as well (you need to be super user to shut down the system).
    I’ve amended the post to include these shorter versions.

  3. Yes indeed… I have always just typed ‘sudo halt’ which seems to do the trick!

  4. I use
    sudo poweroff

    Which to me seems easier to remember!

  5. I’m not sure what’s up but after each ‘sudo reboot’ my pi doesn’t want to come back on the network (wired ethernet). I have to unplug the power and plug it back in order for it to work.
    I didn’t check yet with a display connected to it, I’m doing this over ssh.

  6. Someone interested in a little Android App that can
    – shutdown
    – reboot
    – ping (check if RPi is available in net)
    the RaspberryPi

    I started to create one, but need some tester.
    Other wishes for the app?

  7. For my 11th b-bay my uncle got me this computer (raspberry Pi) and ive heard of it before from my friend ilyah :-) :yes: but now i start it and it makes all these colors green, red, yellow…you get! it then it starts up but ether everthing freezes (or says when its starting up) “start fail” something like that then it says “consle error” or “corruption error” it’s really starting to p*** me off :-X the only cool thing that happened to me today (live in SF) is i saw a space shuttle fly over my school today it was awesome!! :-|

    please email me when u figure out how to fix it PLZZZZZ!!!!! im going crazy :eek: and ive only hade it for 2 days :reallyangry:

    Email: AkalaAiken@gmail.com
    Or Email: SangraalAiken@gmail.com
    Or: TheloAiken@gmail.com

  8. Hello, is there a way to build a remote controller in order to run “sudo shutdow -h now” ?

    • What sort of thing had you in mind? You can login via ssh and do sudo halt from there. There’s even a thread somewhere about someone who has hooked up a shutdown button to one of the GPIO ports which runs a Python script which shuts down the Pi.

      Exactly what sort of remote control were you after? I’m sure it’s possible though.

    • I know this is old but anyway I have a sugestion. I’m using a windows shortcut and putty.
      1) Create a plain text file with the commands (or commands) you want to run remotely. Let’s say c:\users\myself\shutdown.txt with content “sudo shutdown -h now”.
      2) Create a shortcut to putty.exe.
      3) Go to shortcut’s properties.
      4) Edit the “target” to something like this:
      putty.exe -ssh -2 -l user -pw password -m C:\Users\myself\shutdown.txt hostname

      Take into account that you are using the username and password on the shortcut property so anyone with access to your filesystem can see that.
      Hope it works! I’m using it myself as a toolbar on windows:

  9. Hello,

    I plan to create a portable raspberry Pi that will be used in a car. It will be used plugged in but because I will forget to turn off properly the RPi, I plan to add a portable usb charger on battery. Therefore when I turn off the car the RPi will continue to be powered by the battery pack and that’s fine. But the battery is there only to let me turn off the RPi properly not for more. First, is it possible to add a physical button to force a shutdown of the RPi ? Then, do you think is it possible to use the no-more power event from the car to force the shutdown ?

    Regards

    Christophe

    • You can add a shutdown button, people have done this and written about it in the Pi forums.
      You could also detect the “no more power event” and use that to trigger a controlled shutdown as well, but you’d need additional hardware, either a voltage divider, to reduce 5V to 3V3, or a level converter, or an analog to digital converter. It shouldn’t be too hard to do. :)

  10. pleas help i wrote “sudo power off” and now nothings works
    help me please

    • Just remove the power, remove and replace SD card, plug in power and see if it works. If it doesn’t you’ve got a damaged file system (barring other hardware failures) and will need to reflash the SD card.

  11. I have my Pi running raspbian. My issue on this topic is that when I do a sudo reboot, the Pi reboots, but at the reboot, I can no longer SSH into the Pi. If I do a hard reboot such as pulling the power and plugging it back in, it boots and I can SSH in normally. My question is why can I not SSH after doing the sodo reboot?

    Dan B.

  12. shutdown did not work on my Pi, ” shutdown: not found”!

  13. After I used the command “sudo poweroff” it worked! The only problem is now i cannot reboot. I have tried different SD cards and also reloaded the OS a few times. Please help.

    • I wonder if the springy connectors on the SD card slot have got squashed. Is there any way you can try to pull them out a little to make sure of a good connection? The other thing you could try might be to press on the contact end of the SD card while powering up and see what happens.

  14. Useful article, thanks, but I have a strange one. I have used “sudo halt”, “sudo shutdown -h now” “sudo poweroff” and every time my pi seems to be shutting down, but then proceeds to boot back up again so I can’t unplug it without possible damage. Why won’t it switch off?

  15. Hi, after I use “sudo shutdown -h -H now”, pi doesn’t come back on network until i re-image. what could be the cause of that?

  16. works great thank you very much!

Leave a Reply