Detailed analysis of shutdown commands in 🐧 Linux systems

Detailed analysis of shutdown commands in 🐧 Linux systems

Some commonly used shutdown/restart commands under Linux are 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 learn more Flexible use of various shutdown commands.

shutdown

The shutdown command safely shuts down the system. Some users will shut down Linux by directly disconnecting the power supply, which is very dangerous. Because Linux is different from Windows, many processes are running in the background, so forced shutdown may cause the loss of process data, make the system in an unstable state, and even damage hardware devices in some systems. When the shutdown command is used before the system is shut down, 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 shut down after a certain time delay, and it may also restart. This is determined by the signal sent by the system to all processes [process]. This gives programs like vi time to store the document currently being edited, while programs that handle mail (mail) and news (news) can leave normally, etc.

Shutdown performs its job by sending a signal [signal] to the init program, asking it to change the runlevel. Runlevel 0 is used to stop [halt], runlevel 6 is used to reactivate the [reboot] system, and runlevel 1 is used to make the system enter a state where management tasks can be performed; this is the default, assuming no -h There is also no -r parameter for shutdown. If you want to know what actions are done during shutdown (halt) or reboot (reboot), you can see the runlevels related information in this file /etc/inittab.

Shutdown parameter description:

  • [-t] Before changing to another runlevel, tell init how long to shut down.
  • [-r] Restart the calculator.
  • [-k] It does not really shut down, it just 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. The use of this option is discouraged, and the consequences of this option are often not always what you expected.
  • [-c] cancel current process to cancel the currently executing shutdown procedure. So of course there is no time parameter for this option, 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 shutting down [shutdown].     

halt-the simplest shutdown command

In fact, halt is to call shutdown -h. When halt is executed, the application process is killed, the sync system call is executed, and the kernel is stopped after the file system write operation is completed.

Parameter Description:

  • [-n] Prevent the sync system from being called. It is used after patching the root partition with fsck to prevent the kernel from overwriting the patched superblock with the old version of the superblock [superblock].
  • [-w] is not a real restart or shutdown, just write 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. Poweroff is called when shutting down.  

reboot

The working process of reboot is almost the same as halt, but it causes the host to restart, and halt shuts down. Its parameters are similar to halt.

init

Init is the ancestor of all processes, and its process number is always 1, so sending a TERM signal to init will terminate all user processes, daemon processes, etc. Shutdown uses this mechanism. Init defines 8 runlevels, init 0 is shutdown, and init 1 is restart. Regarding init can be a long discussion, I will not go into it here. In addition, there are telinit commands that can change the operating level of init. For example, telinit -iS can make the system enter single-user mode, and the information and waiting time for shutdown are not available.

whoami
Stefan Pejcic
Join the discussion

I enjoy constructive responses and professional comments to my posts, and invite anyone to comment or link to my site.