Click the blue text to follow us

Overview
In Linux systems, shutting down or rebooting a computer is not merely a matter of cutting off the power. To ensure data safety and system stability, Linux provides a series of commands to gracefully terminate all running processes, synchronize data to disk, and finally perform hardware shutdown or reboot operations. Proper use of these commands is fundamental to system administration.
The following is a compilation of shutdown and reboot commands based on online resources and my personal understanding. Please forgive any inaccuracies in my interpretation.
Principles
Permission Requirements: Most shutdown commands require root superuser privileges. Regular users typically use the sudo command to temporarily gain the necessary permissions.
Safety First: Prefer commands that provide warnings and delays, especially in multi-user environments or on servers, allowing other users time to save their work.
Understanding the Underlying Mechanism: In modern Linux systems (such as Ubuntu, CentOS, Fedora, etc.), the shutdown process is primarily controlled by the systemd system and service manager, with traditional commands mostly calling systemctl.
1. Preferred Command: shutdown
The shutdown command is the safest, most flexible, and most recommended way to shut down. It allows scheduling a shutdown time and sending broadcast messages to all logged-in users.
1. Basic Syntax
shutdown [options] [time] [warning message]
2. Common Options
-h: Halt, stop the system. Followed by a time parameter, indicating shutdown.
-r: Reboot, restart the system.
-c: Cancel, cancel the scheduled shutdown task.
3. Time Parameters
now: Execute immediately.
+m: Execute after m minutes (e.g., +10 means after 10 minutes).
hh:mm: Execute at the specified 24-hour time (e.g., 23:00).
4. Common Examples
|
Command |
Usage |
|
shutdown -h now |
Immediately safe shutdown |
|
shutdown -r now |
Immediately safe reboot |
|
shutdown -h 23:00 |
Shutdown at 23:00 tonight |
|
shutdown -r +10 |
Reboot after 10 minutes |
|
shutdown -h +10 “System maintenance, please save your work!” |
Shutdown after 10 minutes and send a warning message |
|
shutdown -c |
Cancel all scheduled shutdown tasks |
|
Safety Mechanism: shutdown first sends a friendly signal (SIGTERM) requesting programs to exit, and after a timeout, it forcibly terminates (SIGKILL), and finally calls systemctl to complete the shutdown, maximizing data integrity. |
2. Other Common Commands
These commands can be seen as shortcuts to shutdown, but lack the flexibility of scheduled time and broadcast messages, making them more suitable for personal desktop environments or scripts.
1. poweroff
Function: Immediately shut down the system and cut off power.
Essentially: Equivalent to shutdown -h now.
Usage:
# poweroff
2. reboot
Function: Immediately restart the system.
Essentially: Equivalent to shutdown -r now.
Usage:
# reboot
3. halt
Function: Stop CPU operation, but not necessarily cut off power. Behavior varies by system, and may ultimately call poweroff.
Note: This is a lower-level command and is not recommended for regular users due to its uncertain behavior.
Usage:
# halt
3. Traditional Commands: init and telinit
init is the core command of the traditional SysV init system, used to switch runlevels. In modern systems based on systemd, these commands are retained for compatibility.
init 0 or telinit 0: Switch to shutdown runlevel (shutdown).
init 6 or telinit 6: Switch to reboot runlevel (reboot).
Usage:
# init 0 //shutdown
# init 6 //reboot
4. Modern System Core: systemctl Command
In distributions that use systemd, the core of system service management is the systemctl command. Most of the above commands ultimately call it to perform their functions.
Shutdown:
# systemctl poweroff
Reboot:
# systemctl reboot
Suspend: (Suspend):
# systemctl suspend
Hibernate: (Hibernate):
# systemctl hibernate
Understanding systemctl is crucial for in-depth management of modern Linux systems.
5.Summary
|
Command |
Function |
Safety |
Applicable Scenarios |
|
shutdown |
Safe shutdown, reboot, can be scheduled |
Very High |
All scenarios, especially servers and multi-user environments |
|
poweroff |
Immediate shutdown and power cut |
High |
Quick shutdown in personal desktop environments |
|
reboot |
Immediate reboot |
High |
Quick reboot in personal desktop environments |
|
systemctl |
System control (shutdown, reboot, etc.) |
High |
Explicit use in system management with systemd |
|
init |
Switch runlevels (shutdown, reboot) |
Medium |
Compatibility with old scripts or systems |
|
halt |
Stop the system |
Low |
Not recommended for regular users |
Personal Recommendations:
1. Prefer shutdown: Always prioritize using the shutdown command, especially in forms with delays and warning messages, as this is a professional and responsible approach.
2. Use sudo: Remember to add sudo before commands to gain the necessary permissions, as these operations require root privileges.
3. Cancel scheduled tasks: If you mistakenly set a timed shutdown, remember to useshutdown -c to cancel it.
4. Exercise caution on servers: Before executing shutdown or reboot on production servers, be sure to confirm if other users are online using the who or w commands, and broadcast notifications in advance to avoid irreversible errors.
Notes:
1. Before forcing a shutdown (such as pulling the power or holding the power button), make every effort to use the above commands for a normal shutdown, as failure to do so may lead to data corruption or filesystem errors.
2. Some commands may vary slightly across different Linux distributions or initialization systems (systemd and init), but the commands described in this article are generally applicable across mainstream distributions.
Long press the QR code to follow us


CSDN BlogδΈ¨Countless Stars in the Sky
https://blog.csdn.net/myself88129