And, a task that seems trivial on any computer, shutting down our Raspberry Pi correctly is of vital importance. This thing doesn’t have a power button!
Under no circumstances will we shut down a Raspberry Pi by removing the power cable. By doing so, we run a huge risk of corrupting the files on the SD card, which requires formatting it and reinstalling 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 correctly helps reduce the possibility of a corrupt SD card (although it does not 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 process at this time.
If instead of shutting it down we want to schedule the shutdown, we can use:
shutdown hours:minutes &
Where we should replace ‘hours’ and ‘minutes’ with the value we want. 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:
On the other hand, we have the command:
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 slight differences. Although they have been smoothed over time, and are not completely applicable to the Raspberry Pi, they are maintained 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 and power management is different.
In the case of Raspbian, the three commands should have the same behavior. However, out of habit and concern about these small differences, and because in other systems they may not be equivalent, I personally recommend the ‘shutdown’ command. It’s better not to develop bad habits.
Restart Raspberry Pi
Restarting the system is just as simple as shutting it down. Again we use the ‘shutdown’ command but adding the ‘-r’ parameter, as follows:
sudo shutdown -r now
Similar to what we have seen about shutting down Raspberry Pi, we have another alternative command to restart it:
Just like in the previous case, the behavior in Raspbian should be similar to using ‘shutdown -r’. But, because of the differences and good practices, it is recommended to use the ‘shutdown -r’ command.
Log out user
Related to shutting down and restarting the Raspberry Pi, if we only want to log out the current user, we just have to use the command:
Shutting down or restarting Raspberry Pi properly is fundamental for the correct maintenance of our system. We have seen that it is very simple thanks to the ‘shutdown’ command, with its different parameters.
In the next entries in the Raspberry Pi series, we will continue to delve into the details of Raspbian. See you soon!