How To Restart Raspberry Pi Safely And Quickly

James J. Davis
5 min readDec 10, 2020

--

To avoid damaging the file system on the SD card, you should always shut down and reboot the Raspberry Pi properly (not just unplug the power cord).

To safely shut down or reboot your Raspberry Pi through the GUI, simply find the appropriate buttons in the menu.

In this post, I will show you how to restart raspberry pi from the command line (or you can see video lessons)

When RPi (or any other UNIX-like system) shuts down or reboots correctly, using commands from the terminal or their GUI equivalents, it first tries to shut down all gracefully processes by sending them a SIGTERM signal, which notifies them to save data and shut down. After a while, to all remaining processes, it sends SIGKILL, unmounts all file systems and sends the appropriate command to the ACPI power management interface.

When RPi (or any other UNIX-like system) shuts down or reboots correctly, using commands from the terminal or their GUI equivalents, it first tries to shut down all gracefully processes by sending them a SIGTERM signal, which notifies them to save data and shut down. After a while, to all remaining processes, it sends SIGKILL, unmounts all file systems, and sends the appropriate command to the ACPI power management interface. This is done by invoking the shutdown(8) program.

What if, instead of shutting down gracefully after sending SIGTERM, we tell our RPi to just reboot? And what happens if we do this without hitting control-alt-delete first. We might find that a few processes still hang around, or nothing seems to happen at all, or that a few files have been corrupted! This is because the usual shutdown sequence has not happened and so some services don’t stop in time and leave locks on disk which other services can’t see when you reboot. In addition file system integrity cannot be guaranteed when booting in this way, unless you add it explicitly as an option to your /etc/fstab for instance.

The latter is a good idea for systems that aren’t expected to be turned off regularly.

The solution? Well, we could use a system that can recognize when it is shutting down or rebooting and then do the right thing, but there are already some tools out there that will do this for us if we give them the chance. They are called Init Replacement distributions.

One method acts as an intermediary between the kernel’s power management functions and the other services on the system — It receives commands from userspace telling it to shut down or reboot, waits until no processes are using files anymore (e.g.: no open file descriptors) before unmapping all memory mappings of processes, sends a SIGTERM to all processes and only after they have shut down, tells the kernel to reboot or shutdown. This method ensures that processes have time to clean up so you can be more confident that the machine will boot again successfully following an ungraceful shutdown, but it has drawbacks — if any process is using files when you attempt to shut down the system (e.g.: open sockets in services like httpd), then you may get lockups or corruptions.

Turn off the Raspberry Pi

Use any of the above commands to turn off the RPi correctly:

$ sudo halt
$ sudo poweroff
$ sudo shutdown -h now
$ sudo shutdown -h 10 # after 10 minutes
$ sudo init 0

Once the shutdown process is complete, the green LED on the Raspberry Pi board will blink a few times — it is then safe to disconnect the power cable.

It’s okay if only the red LED stays on — it means that the Raspberry Pi is connected to a stable power source.

Safely reboot Raspberry Pi

Execute any of the following commands to safely reboot the RPi:

$ sudo reboot
$ sudo shutdown -r now
$ sudo shutdown -r 10 # after 10 minutes
$ sudo init 6

But if you can’t wait to see what happens when you power up the device again, here’s an interesting experiment.

First, make sure you have a good power supply and connect everything as explained in the Quick Start Guide. Now boot your Raspberry Pi for about 10 seconds just long enough to log onto the screen. Once you’re on type this command sudo shutdown -r now. You’ll see the light on the Pi starts to flicker as it reboots, but this time instead of turning it off it will restart again and again.

What’s happening is almost certainly a hardware problem. The Broadcom system on chip (SoC) device contains several “cores” that communicate with each other via an internal memory bus. This is not to be confused with the main system memory, which can also be accessed by the cores, and in fact, it’s through here that the Linux kernel starts its initial boot process.

That memory access has gone wrong and as a result, the kernel is unable to start properly. Under normal circumstances, that error would be detected by the OS and it would shut itself down so as not to cause further damage. However, in this case, because there’s no OS loaded it doesn’t know what to do next so the only option is to keep rebooting.

How do I restart my Raspberry Pi from a remote system?

Something like this might help:

ssh pi@192.168.1.XXX 'echo raspberry | sudo shutdown -r now'

this command connects to your Raspberry using SSH and issues a restart command, make sure you replace 192.168.1.XXX with the real address.

Have fun guys & girls!

You might also want to know other tricks with RPi:

--

--