1. Five Linux Restart Commands
- shutdown
- poweroff
- init
- reboot
- halt
2. Detailed Descriptions of Four Restart Commands
2.1 shutdown
The shutdown
command safely shuts down the system. Some users may directly cut off the power to shut down Linux, which is very dangerous. Unlike Windows, Linux has many processes running in the background, so forcibly shutting down can result in data loss for these processes, making the system unstable, and in some systems, it may even damage hardware devices. Using the shutdown
command before shutting down the system allows the system administrator to notify all logged-in users that the system is about to shut down. Additionally, the login command will be frozen, preventing new users from logging in. The shutdown
can either happen immediately or be delayed, and it can also lead to a reboot. This is determined by the signals sent to all processes.
Shutdown
executes its task by sending signals to the init
program, requesting it to change the runlevel.
- Runlevel 0 is used for halting the system.
- Runlevel 6 is used for rebooting the system.
- Runlevel 1 is used to bring the system into a state where administrative tasks can be performed.
This is the default behavior, assuming neither the -h
nor the -r
parameter is given to shutdown. To understand what actions are performed during halt or reboot, you can refer to the related information in the file /etc/inittab
.
shutdown command options:
- [-t]: Specifies the delay before changing to another runlevel.
- [-r]: Reboots the system.
- [-k]: Does not actually shut down but sends a warning signal to all logged-in users.
- [-h]: Halts the system after shutting down.
- [-n]: Does not use init but shuts down directly. This option is discouraged as its consequences are often unpredictable.
- [-c]: Cancels the current shutdown process. This option does not have a time parameter, but you can input a message for explanation, which will be sent to all users.
- [-f]: Ignores fsck during reboot.
- [-F]: Forces fsck during reboot.
- [-time]: Sets the time before shutdown.
2.2 halt
- The Simplest Shutdown Command
Halt
is essentially calling shutdown -h
. When halt is executed, it kills application processes, executes the sync system call, and after the file system write operations are completed, it stops the kernel.
Options:
- [-n]: Prevents the sync system call. It is used after repairing the root partition with fsck to prevent the kernel from overwriting the repaired superblock with an old version.
- [-w]: Does not actually reboot or shut down but just writes the wtmp (/var/log/wtmp) record.
- [-d]: Does not write the wtmp record (included in the [-n] option).
- [-f]: Forces a shutdown or reboot without calling shutdown.
- [-i]: Shuts down (or reboots) after closing all network interfaces.
- [-p]: This option is the default, calling poweroff during shutdown.
2.3 reboot
The reboot command works similarly to halt, but it triggers a system reboot instead of a shutdown
. Its options are similar to those of halt.
2.4. init
Init
is the ancestor of all processes with a process ID of 1. Sending a TERM signal to init
will terminate all user processes, daemon processes, etc. The shutdown command uses this mechanism. Init
defines 8 runlevels:
- init 0 shuts down the system.
- init 1 reboots the system.
Additionally, the telinit
command can change the runlevel of init
. For example, telinit -iS
can put the system into single-user mode, without the notifications and waiting time associated with using shutdown
.
Comment Section