How can you shutdown Linux from the command line? Let’s learn to use the shutdown command in Linux for the same purpose!
A Linux system admin is in charge of keeping a system and running and maintaining updates on the machine. But for core kernel updates, your system needs to be shut down safely and restarted.
You also need to let all the currently logged in users know that the system is about to be shut down. This gives them the chance to save important files before the system goes offline.
This further prevents them from losing data, which might happen in case the system goes down suddenly. To take care of these requirements, we make use of the shutdown command in Linux.
Why does the shutdown command in Linux exist?
The shutdown command in Linux command is a safe way to terminate running processes on a system and shut it down. It can be used to shut a system down either immediately or at a scheduled time.
When a shut down is initiated, all the users who are currently logged on to the system are notified about it. Further, the system prevents any new user from logging on.
All the running process receive a SIGTERM kill signal (read more about termination signals here) so that they can be terminated gracefully.
Next, the shutdown command in Linux signals to the init command to modify the runlevel for the system.
The runlevel 0 sends the system into the halt stage, runlevel 1 makes the system work in a single user mode while runlevel 6 reboots the system.
Understanding the shutdown command
The best way to understand any command is through understanding it’s syntax. Here is how the syntax for the shutdown command in Linux looks like.
shutdown [option] [time] [message]
Here, the time can refer to a specified time (based on a 24-hour clock) or a period from the time of initiating the command. The ‘message’ specifies the message which the users will receive when they are notified about the shutdown being initiated.
The shutdown command allows several options to dictate how you initiate a shutdown signal. Here is a list of some of the commonly used options offered by the shutdown command in Linux.
|-r||Reboots the system|
|-k||Sends shutdown warning to all logged-in users without actually shutting down|
|-h||This option is used to tell the command to halt the system. In the case of ‘halt’, all the running processes are terminated and the system is shut down.|
|-P||Emulates the power button shut down by sending a system power-off signal.|
|-c||Cancel scheduled shutdowns|
While this list covered the basic options used in the shutdown command, you should explore the other options to understand its full potential.
Using the shutdown command in Linux
Now we have developed an understanding of the shutdown command in Linux along with its parameters. It’s time to use this knowledge for practical application of the shutdown command.
For this tutorial, we will go over some examples to learn how to use the shutdown command. Note that the shutdown command in Linux can only be initiated by the root user.
Scheduling the shutdown
The most basic use of the shutdown command in Linux is to schedule a shutdown. For this example, we will schedule our system to perform the shutdown at 5 PM. Type the following in your command line.
Note that we need to enter the time in a 24-hour format. entering 5:00 here will schedule the shutdown for 5 AM instead of 5 PM.
Initiating the shutdown with a message
When you initiate a shutdown, the users who are logged on the system might need to be informed about the reason for the shutdown. For this task, we make use of a message. This can be done as shown below.
shutdown +20 "The system will be down in 20 minutes for hardware upgrades"
As you can notice, the users will receive a custom message specifying the reason for the shutdown. Further, we used +20 instead of a specified time in this command. This tells the compiler to shut down the system 20 minutes after the command is entered into the terminal.
Restarting the system
Sometimes the system just requires a quick reboot. This can be done using the ‘-r- option of the shutdown command on your system. The -r command, as we discussed in the table earlier, would execute a proper shutdown.
Then it will immediately signal the system to reboot itself. This command can be written as the following.
shutdown -r now shutdown -r 17:00
Notice that we replaced the time with the keyword ‘now’. This keyword will cause the shutdown command to be executed immediately, without waiting for any other input.
Also, you can see how I’ve specified the time right after the command to schedule that specific action, which in this case is restart.
Powering off the system
When you wish to turn off your system and disconnect it from the power supply, we use the ‘-P’ option of the shutdown command. This can be done as follows.
shutdown -P now shutdown
This will shut down the system and send an ACPI signal to the PSU to cut off the power supply for our system.
The shutdown command is an important tool for Linux system admins as it allows the root user to halt, reboot and power off their system or schedule the process.
It also has basic defenses against sudden shutdowns as all processes are terminated gracefully before the system is turned off.
We hope this tutorial was able to help you understand the shutdown command in Linux. If you have any feedback, queries or suggestions, feel free to reach out to us in the comments below. For further details, read through the shutdown command manpage.