Commonly used shutdown commands under Linux are: shutdown, halt, poweroff, and init; the restart commands are: reboot. The following article mainly introduces some commonly used shutdown commands as well as the differences and specific usages of various shutdown commands.
First, let’s take a look at some of the more commonly used shutdown commands under Linux
Shutdown commands:
1. Halt Shut down immediately 2. Poweroff Shut down immediately 3. Shutdown -h now Shut down immediately (for root users) 4. Shutdown -h 10 Automatic shutdown after 10 minutes. If the shutdown is set through the shutdown command, you can use the shutdown -c command to cancel the restart. Restart command:
1. reboot 2. shutdown -r now to restart immediately (for root users) 3. shutdown -r 10 Automatically restart after 10 minutes (for root users) 4. shutdown -r 20:35 Restart when the time is 20:35 (for root users) If the restart is set through the shutdown command, you can use the shutdown -c command to cancel the restart
Let’s take a look at the differences between these specific shutdown commands in Linux and their respective usages
1. shutdown safe shutdown command
For the shutdown command, it is a safe command recommended by everyone, through the parameters - Use h or -r to complete the shutdown or restart. However, in Linux systems, this command can only be used with root privileges. Therefore, although everyone recommends using this command, it is really inconvenient to use: Do you want to use this command? First go and obtain root privileges. Shutdown performs a shutdown by sending a signal to init, asking it to change the run level to shut down the machine. Shutdown or restart is actually an adjustment of the run level, so we can also use init to directly adjust the run level to shut down or restart. When using this command, the machine shuts down or restarts immediately. It also requires root access.
So why is the shutdown command safe to shut down the system?
In fact, some users will shut down Linux by directly cutting off the power supply, which is very dangerous. Because Linux is different from Windows, there are many processes running in the background, so forced shutdown may cause data loss of the process and put the system in an unstable state. It can even damage hardware devices in some systems. If you use the shutdown command before shutting down the system, the system administrator will notify all logged-in users that the system will be shut down. And the login command will be frozen, that is, new users can no longer log in. It is possible to shut down directly or delay shutdown for a certain period of time, or restart. This is determined by the fact that all processes will receive signals sent by the system.
Shutdown performs its job by sending a signal [signal] to the init program, asking it to change the runlevel. Runlevel 0 is used to shut down (halt), runlevel 6 is used to reactivate (reboot) the system, and runlevel 1 is used to put the system into a state where management work can be carried out. This is the default. It is assumed that neither -h nor -r parameters are given to shutdown. Want to know what actions are taken during the shutdown (halt) or restart (reboot) process? You can see these runlevels-related information in this file /etc/inittab.
shutdown parameter description:
[-t] tells init how long to shut down before changing to other runlevels. [-r] Restart the calculator. [-k] does not actually shut down, but only sends a warning signal to each login [login]. [-h] Turn off the power after shutdown [halt]. [-n] Do not use init but shut down by yourself. Using this option is discouraged, and the consequences of this option are not always what you expect. [-c] cancel current process cancels the shutdown program currently being executed. So of course this option has no time parameter, but you can enter a message to explain it, and this message will be sent to each user. [-f] Ignore fsck when restarting the calculator [reboot]. [-F] Force fsck when restarting the calculator [reboot]. [-time] Set the time before shutdown [shutdown]. 2.halt The simplest shutdown command
When using the halt command to shut down, the actual call is shutdown -h. When halt is executed, the application process will be killed, and the kernel will be stopped after the sync system call file system write operation is completed.
halt parameter description:
[-n] prevents sync system calls. It is used after patching the root partition with fsck to prevent the kernel from overwriting the patched superblock with an older version of the superblock. [-w] is not a real restart or shutdown, but just writes wtmp [/var/log/wtmp] records. [-d] Do not write wtmp records [included in option [-n]]. [-f] Force shutdown or restart without calling shutdown. [-i] Turn off all network interfaces before shutting down (or restarting). [-p] This option is the default option. Just call poweroff when shutting down.
3.poweroff is a commonly used shutdown command
For poweroff, it is said on the Internet that it is a link to the halt command. The basic usage is similar to halt, so I won’t go into details here.
4.init
init is the ancestor of all processes. It is one of the indispensable programs in Linux system operation. Its process number is always 1, so sending the TERM signal to init will terminate all user processes, daemon processes, etc. shutdown uses this mechanism. init defines 8 runlevels (runlevel), init 0 is shutdown, init 1 is restart.
5.reboot Restart command
The working process of reboot is almost the same as halt. However, it causes the host to restart, while halt causes a shutdown. Its parameters are similar to halt.
For more Linux shutdown command summary related articles, please pay attention to the PHP Chinese website!