como-apagar-o-reiniciar-correctamente-raspberry-pi

How to properly shut down or restart Raspberry Pi

  • 3 min

Shutting down a Raspberry Pi properly means telling the operating system to close processes and sync data before cutting the power.

It is a task that seems trivial on any computer, but on Raspberry Pi it is especially important. This thing normally has no power button, and the SD card does not handle having the lights turned off on it without warning particularly well.

Under no circumstances should we turn off a Raspberry Pi by unplugging the power cable. Doing so runs a huge risk of corrupting the files on the SD card, which forces us to format it and reinstall from scratch.

In fact, the alarming tendency to corrupt the SD card is one of the biggest problems with the Raspberry Pi. Shutting it down properly helps reduce the chance of a corrupted SD card (although it doesn’t eliminate it completely).

Shut Down Raspberry Pi from Command Line

The correct way to shut down a Raspberry Pi from the command line is to use the command:

sudo shutdown -h now

Where -h corresponds to the halt option and now indicates that we want to start the shutdown at this moment.

If instead of shutting it down we want to schedule the shutdown, we can use:

shutdown hours&

Where we must replace hours and minutes with the desired value. If we want to cancel a scheduled shutdown with the previous command, we use:

sudo shutdown -c

There are other similar commands to shut down the Raspberry Pi. This leads to some confusion, so let’s look at them. The first alternative is to use the command:

sudo halt

On the other hand, we have the command:

sudo poweroff

Which performs the same functions as halt, but also shuts down the system, turning off the PSU if the board has ACMI. Since the Raspberry Pi does not have a PSU, in this case it is equivalent to halt.

Historically, these commands (shutdown, halt, and poweroff) had small differences. Although they have softened over time and are not entirely applicable to the Raspberry Pi, they remain in certain systems.

We could summarize that shutdown -h corresponds to a normal shutdown, while halt and poweroff correspond to a more abrupt emergency stop. In general, the behavior regarding process termination and power management is different.

In Raspberry Pi OS the three commands usually end in a similar result. However, out of habit and because in other systems they may not be equivalent, I personally recommend the shutdown command. It is better not to pick up bad habits.

Reboot Raspberry Pi

Rebooting the system is just as simple as shutting it down. Again we use the shutdown command but adding the -r parameter, like this:

sudo shutdown -r now

Similar to what we have seen about shutting down the Raspberry Pi, we have another alternative command to reboot it:

sudo reboot

Just as in the previous case, the behavior should be similar to using shutdown -r. As a matter of good habit, it is recommended to use the shutdown -r command.

Log Out User

Related to shutting down and rebooting the Raspberry Pi, if we only want to log out the current user, we simply have to use the command:

sudo logout

With this, we can now shut down and reboot Raspberry Pi without playing Russian roulette with the SD card. It may seem minor, but it is one of those good habits that save a lot of trouble.