In this blog we will see how to boot, reboot, and shutdown a system safely in linux.
Reboot and Shutdown
To reboot or shutdown a Linux machine we’ll often use the systemctl (system control) command.
Some commands require system administrator privileges. The root user has such privileges. So we can reboot a machine by simply typing systemctl reboot. Regular users cannot use commands that change the system’s state. But they can temporarily get root privileges, if they add sudo in front of their commands. So a regular user needs to type sudo systemctl reboot instead.
All you need to remember is this: “If I am logged in as root, I don’t need sudo”. So you can skip writing the “sudo” word if you’re already logged in as root. This applies to all other examples in these lessons where you see sudo.
sudo systemctl reboot
sudo systemctl poweroff
Rarely, you might find yourself in situations where the system refuses to reboot or shutdown normally. That might be because some program is misbehaving, stuck in some way and it does not want to close properly. In such abnormal situations, you can force close all such programs and reboot in a more abrupt way (not recommended to do unless absolutely necessary).
sudo systemctl reboot --force
sudo systemctl poweroff --force
If not even this works, you can specify –force twice (only use as last resort):
sudo systemctl reboot --force --force
This is exactly like pressing the reset button. The system reboots instantly, programs have no chance to close properly or save their data.
systemctl poweroff --force --force
This is exactly like unplugging a computer from its power source.
Scheduling Reboot or Shutdown
You’ll often find that you need to reboot some servers in the middle of the night, say 2 or 3AM. It’s inconvenient to have to wake up just to reboot some device, so you can instead instruct Linux to do this on its own.
The shutdown command is better suited for scheduled reboots or shutdowns.
To shutdown at 02:00 AM:
sudo shutdown 02:00
The time is in 24-hour format, so you can use anything between 00:00 and 23:59.
If you want to shutdown x minutes later, use +x instead. To shutdown after 15 minutes:
sudo shutdown +15
To reboot instead, add the -r, reboot option:
sudo shutdown -r 02:00
sudo shutdown -r +15
You can also set what is called a wall message. If a few users are logged in to this Linux machine, the wall message will be shown to them before the server reboots or shuts down. This gives them a chance to know in advance why and when the machine will become unavailable. It also gives them a chance to finish their work before this happens, instead of abruptly being disconnected while having no idea what happened.
You can write your wall message like this (between ‘ ‘ quotes):
sudo shutdown -r +1 'Scheduled restart to do an offline-backup of our database'
To learn more about Linux checkout our hands-on Linux course here
To get free hands-on experience on System Administration and DevOps tasks please check out our KodeKloud Engineer program here