Using the "shutdown" command (with examples)
- Linux
- November 5, 2023
The “shutdown” command is used to power off or reboot a system. It provides several options for scheduling shutdown or reboot operations, allowing users to specify the exact time or delay. In this article, we will explore different use cases of the “shutdown” command and provide code examples for each one.
1: Power off (halt) immediately
shutdown -h now
Motivation: This use case is helpful when you need to shut down the system immediately, without any delay. It can be useful in emergency situations or when you want to quickly power off the system.
Explanation: The “-h” option specifies the action to be taken as “halt,” which means to power off the system. The “now” keyword indicates that the action should be performed immediately.
Example output: The system will initiate an immediate shutdown, and all processes will be terminated gracefully before the system powers off.
2: Reboot immediately
shutdown -r now
Motivation: Rebooting a system can be required when installing new software or making significant system changes. This use case allows you to perform a reboot operation immediately without any delay.
Explanation: The “-r” option specifies the action to be taken as “reboot,” which means to restart the system. The “now” keyword indicates that the action should be performed immediately.
Example output: The system will initiate an immediate reboot, and all processes will be terminated gracefully before the system restarts.
3: Reboot in 5 minutes
shutdown -r +5 &
Motivation: Sometimes, it may be necessary to schedule a system reboot for a specific time in the future. This example demonstrates how to schedule a reboot operation after a delay of 5 minutes.
Explanation: The “-r” option specifies the action to be taken as “reboot,” which means to restart the system. The “+5” argument indicates a delay of 5 minutes before the action is performed. The “&” symbol at the end allows you to run the command in the background, freeing up the terminal for other tasks.
Example output: The system will display a warning message notifying users about the scheduled reboot. After 5 minutes, the system will initiate the reboot, allowing users to save their work or perform any necessary actions before the restart.
4: Shutdown at 1:00 pm (Uses 24h clock)
shutdown -h 13:00
Motivation: There may be instances when you need to schedule a system shutdown at a specific time, such as at the end of the workday or during maintenance windows. This use case illustrates how to schedule a shutdown operation for 1:00 pm.
Explanation: The “-h” option specifies the action to be taken as “halt,” which means to power off the system. The “13:00” argument indicates the time when the system should be shut down. The time should be specified using the 24-hour clock format.
Example output: The system will display a warning message notifying users about the scheduled shutdown. At 1:00 pm, the system will initiate the shutdown, allowing users to save their work or perform any necessary actions before the power off.
5: Cancel a pending shutdown/reboot operation
shutdown -c
Motivation: In some cases, you may realize that a scheduled shutdown or reboot operation is no longer necessary. This example demonstrates how to cancel a pending shutdown or reboot operation.
Explanation: The “-c” option is used to cancel a pending shutdown or reboot operation. When executed, it will stop the countdown timer or scheduled action.
Example output: If there is an active pending shutdown or reboot operation, the system will display a message confirming the cancellation of the scheduled action. Otherwise, if there is no pending operation, the command will have no effect.
By understanding and utilizing the various options of the “shutdown” command, you can have better control over system shutdown and reboot operations. Whether you need to react quickly, schedule future actions, or cancel pending operations, the “shutdown” command provides the flexibility to manage system power gracefully.