Shutdown and Reboot Linux Systems From the Terminal


Introduction

In this article, we will discuss how to shut down and restart Linux systems from the terminal. The ability to shut down and restart a system from the command line can be useful in a variety of situations, such as when the GUI is unavailable or when automating tasks via Linux shell scripts.

Overview

Linux is a fairly robust operating system, and as such, rebooting Linux servers is rarely necessary. However, sometimes there are reasons why you need to restart your system.

For example, if we are running Linux on our personal computer, restarting and shutting down the system could be a daily operation.

Rebooting or Shutting Down a System

Various commands can restart or shut down a system, such as shutdown, restart, shutdown, and shutdown. Since restarting or shutting down a system will kill all running processes, you must have root or sudo permission to run such commands.

Most modern Linux distributions have adopted “systemd” as their system and service manager. On systemd-managed systems, “systemctl” centrally controls system restart and shutdown.

For example, let's look at those commands on an Ubuntu system −

$ file /sbin/{halt,poweroff,reboot,shutdown}
/sbin/halt: symbolic link to /bin/systemctl
/sbin/poweroff: symbolic link to /bin/systemctl
/sbin/reboot: symbolic link to /bin/systemctl
/sbin/shutdown: symbolic link to /bin/systemctl

It might be a little surprising to learn that all of these commands are linked to the same command: “systemctl”. This is because the “systemctl” program uses a technique to change its behavior based on how it was run.

The shutdown command

We can use the shutdown command to safely reboot or shut down a Linux system. The syntax of using the shutdown command is simple −

$ shutdown [OPTIONS...] [TIME] [MESSAGE]

Common Options

Now let's look at the common options supported by the shutdown command. We can pass the “-r” option to reboot our system instead of shutting down the machine −

# shutdown -r
Shutdown scheduled for Thur 2022-01-19 11:56:50 IST, use 'shutdown -c' to cancel.

The “-H” option tells the shutdown command to halt the system −

# shutdown -H
Shutdown scheduled for Thur 2022-01-19 11:56:50 IST, use 'shutdown -c' to cancel.

Schedule a shutdown operation

Sometimes we don't want to shut down or restart the system in one minute. Instead, we can schedule the shutdown operation at a specific time. The shutdown command supports two different time formats for scheduling −

  • absolute time − HH:mm

  • relative time − +NumberOfMinutes

For example, we can schedule a reboot of the system for 8:08 AM

# shutdown -r 08:08
Shutdown scheduled for Thur 2022-01-19 08:00:00 IST, use 'shutdown -c' to cancel.

Let's look at another example where we schedule a system shutdown in 30 minutes −

# shutdown +30
Shutdown scheduled for Thur 2022-01-19 12:36:55 IST, use 'shutdown -c' to CEST, use 'shutdown -c' to cancel.

If we want to shut down the system immediately, we can use “+0” or a convenient alias, “now” −

# shutdown now

Since Linux is a multi-user operating system, we might want to broadcast a message to all currently logged in users about the scheduled shutdown or restart −

# shutdown -r +45 "Attention: This system will restart in 45 minutes!"
Shutdown scheduled for Thur 2022-01-19 12:48:39 IST:...

Cancel a Scheduled Shutdown

In the above examples, we have scheduled a reboot or shut down operation. However, there are times when we might want to cancel a scheduled operation. For example, if a reboot or shutdown was scheduled by mistake, or if a critical update needs to be installed before the scheduled reboot. In such cases, we can use the 'shutdown -c' command to cancel a scheduled operation.

# shutdown -c

This command will cancel any scheduled reboot or shut down operation and display a message confirming the cancellation.

Conclusion

In conclusion, we can use the 'shutdown' command with different options and time parameters to schedule the shutdown or restart of Linux systems. The 'halt' and 'poweroff' commands can also be used to halt or shut down the system, respectively. However, it is always important to use these commands with caution, as they can lead to data loss if not done correctly. It's also important to note that most modern Linux distributions use “systemd” as their system and service manager, and all shutdown commands are tied to the same “systemctl” command. Understanding the behavior of these commands and the switches that can be used with them can help us control the shutdown and restart process of our Linux systems more effectively.

Updated on: 13-Feb-2023

472 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements