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

  44 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?

    • Mr. Knaggs:

      I have seen that phenomenon only once, when I was trying to power the Pi from one of those “USB chargers” that you can use to charge a cellphone if your AC power is out. Apparently, when the Pi shuts down, this particular model of charger dropped its output voltage and then brought it back up again. The Pi assumed that it had just been plugged in again and started to reboot … and so ad infinitum. I eventually had to yank the power cord and then “fsck” the card.

      Have you tried using a different USB power supply?

  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!

  17. This isn’t terribly useful if you can’t get into the Pi. I need mine to be on all the time in order to use it, but I regularly have to unplug it in order to actually connect to it via SSH. I get that it’s a cheap computing device, but would it really have been that expensive to give it a power button? As it is I have to unplug it regularly because it stops accepting connections after a while.

  18. i just got mine and did this to turn it off how do i turn it back on? (im a beginner so please layman term’s)

  19. Just a quick question from a Noobie here. I have a Pi 3 and when I want to shut down so far I have used the latest Rasperian GUI which offers a shutdown and reboot option. It seems to shut the system down, but should this be used or should I open the linux command line and do the sudo halt?

    Thanks in advance for any resopnse.

  20. I deployed Simulink model from my notebook to RPi. So it ran as a standalone application – if I turned off Simulink and Matlab the model was still running on RPi. My question is this: how can I stop that Simulink model when Matlab and Simulink are turned off? (I know how to do it when Matlab is turned on …) But without turning RPi off … Can I somehow use Putty for that (or CLI on RPi for that matter)? I failed so far …

    • Ok, I solved it. If anyone cares, I used Putty: I entered the command “top”, searched for my simulation model’s name, identified its PID, pressed “q”, and used “sudo kill PID”; sudo because the user was named “root” …

  21. Thanks for the response I thought as much, but it is nice to have it verified.

  22. I am setting up a temporary power supply for my RaPi in case the AC supply switches off. In case I am not at the RaPi for a while is it possible for a Python sketch, opened up at Boot, save & then lose any open files before shutting down the RaPi? I know I should have saved everything before I left the RaPi but ….. I can shutdown down the RaPi but not the files after saving.
    While using a RaPi3 I have noticed that, if I put in a ‘sudo reboot’ in an LxTerminal with an open LibreOffice file, when I open that file in LibreOffice after the reboot a ‘LibreOffice Document Recovery’ window will open but any recent modifications will not have been saved. Somehow LibreOffice can recognise that it was not shut down correctly.
    Thanks, in advance, for any help

  23. I made shut-down to my raspberry pi 3 ,and after 1 day I tried to make it work again but i don’t have anything on the screen. What I have to do to make it working again?!

  24. Brand new Raspberrt Pi 4. Booted up first time, formatted the SD card, came up, connected to internet, did a software update, worked fine, did some browsing. Shut the system down (with the GUI). Screen went blank. Never came back up again. How do you start it? Tried power off and on, tried hoding shift key while powering on, tried removing the SD and powering up, tried shift key while SD is out, SCREEN STAYS BLANK. Never stars up again. What do I do?

Leave a Reply