Some commonly used shutdown/restart commands under Linux include shutdown, halt, reboot, and init. They can all achieve the purpose of restarting the system, but the internal working process of each command is different. Through the introduction of this article, I hope you can Use various shutdown commands more flexibly.
1. The shutdown command safely shuts down the system.
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 the data of the process to be lost, put the system in an unstable state, and even damage the hardware equipment 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 will no longer be able to log in. It is possible to shut down directly or delay the shutdown for a certain period of time, and it is also possible to restart. This is determined by the fact that all processes will receive signals sent by the system. This gives programs like vi time to save the document currently being edited, and programs like mail and news can exit normally, etc. Shutdown performs its job by sending a 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 the system [reboot]
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. If you want to know what actions were taken during the shutdown (halt) or reboot (reboot), you can see the runlevels-related information in this file /etc/inittab. Shutdown parameter description:
[-t] in Before changing to other runlevels, tell init how long to shut down.
[-r] Restart the calculator.
[-k] It does not actually shut down, but only sends a warning signal to each login.
[- h] Turn off the power after shutdown [halt].
ded. c] cancel current process to cancel the currently executing shutdown process. So of course this option has no time parameter, but you can enter a message for explanation, 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
In fact, halt is to call shutdown -h. When halt is executed, it kills the application process, executes the sync system call, and stops the kernel after the file system write operation is completed.
[ -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 reboot or shutdown. , just write the wtmp [/var/log/wtmp] record.
dedded in the option [-n]]』 .
[-i] Turn off all network interfaces before shutting down (or restarting).
[-p] This option is the default option.
3. The working process of reboot is almost the same. Halt is the same, but it triggers a host restart, while halt is a shutdown. Its parameters are similar to halt
4.init is the ancestor of all processes, and its process number is always 1, so sending the TERM signal to init will terminate it. All user processes, daemons, etc. shutdown uses this mechanism. Init defines 8 runlevels. Init 0 is shutdown. . There is also the telinit command that can change the running level of init. For example, telinit -iS can make the system enter single-user mode, and the information and waiting time when using shutdown cannot be obtained.
Detailed explanation of init
1. INIT: init is one of the indispensable programs in Linux system operation.
The so-called init process is a user-level process started by the kernel. After the kernel starts itself (has been loaded into memory, started running, and has initialized all device drivers and data structures, etc.), it completes the boot process by starting a user-level program init. Therefore, init is always the first process (its process number is always 1). The kernel looks for init in several places where it has been used in the past. Its correct location (for Linux systems) is /sbin/init. If the kernel cannot find init, it will try to run /bin/sh, and if that fails, the system will fail to boot.
2. Run level
Simply put, the run level is the functional level at which the operating system is currently running. The levels range from 1 to 6 and have different functions. The definitions of different run levels are as follows: (You can refer to /etc/inittab in Red Hat Linux)
# 0 - shutdown (never set initdefault to 0)
# 1-single-user mode
#2-more User, no NFS
# 3 - Full multi-user mode (standard runlevel)
# 4 - Not used
# 5 - X11 (xwindow)
# 6 - Restart (do not set initdefault For 6)
These levels are specified in the /etc/inittab file. This file is the main file that the init program looks for. The first service to run is the file placed in the /etc/rc.d directory. In most Linux distributions, the startup script is located in /etc/rc.d/init.d. These scripts are linked to the /etc/rc.d/rcn.d directory using the ln command. (n here is run level 0-6)
3. Run level configuration
Run level configuration is performed in the /etc/inittab line, as shown below:
12: 2: wait: / etc / init.d / rc 2
The first field is an arbitrarily specified label;
The second field indicates which level this line applies to running (here it is 2);
The third field indicates when entering the run level , init should run the command in the fourth field once, and init should wait for the command to end. The /etc/init.d/rc command runs any commands required to start and terminate input in order to enter runlevel 2.
The command in the fourth field performs all the "chores" when setting the run level. It starts services that are no longer running and terminates services that should no longer run within the new runlevel. Depending on the Linux version, the specific commands used are also different, and the run-level configuration is also different.
When init starts, it will look for a line of code in /etc/inittab. This line specifies the default run level: id: 2: initdefault:
You can ask init to enter a non-default run level when it starts, This is accomplished by specifying a "single" or "emergency" command line argument to the kernel. For example, the specification of kernel command line parameters can be performed through LILO. This way, you can select single-user mode (i.e. runlevel 1). While the system is running, the telinit command can change the run level. When the run level changes, init will run the corresponding command from /etc/inittab.
4. Special configurations in /etc/inittab
There are several special features in /etc/inittab that allow init to reactivate special events. These special features are marked with special keywords in the third field. For example:
1. powerwait Allow init to shut down the system when the power is cut off. The prerequisite is having a UPS and software that monitors the UPS and notifies init that power has been cut off.
2. ctrlaltdel Allows init to restart the system when the user presses the C t r l + A l t + D e l key combination on the console keyboard. Note that if the system is placed in a public place, the system administrator can configure the C t r l + Alt + Del key combination to have other behaviors, such as ignoring it.
3. sysinit Command to be run when the system starts. For example, this command will clear /tmp.
The list of special keywords above is not complete yet. For details on other keywords and their usage, refer to your inittab man page.
For more detailed explanations of Linux shutdown commands and init details, please pay attention to the PHP Chinese website for related articles!