6.6 Shutting Down Your System
init controls how the system shuts down and reboots. The commands to shut down the system are the same regardless of which version of init you run. The proper way to shut down a Linux machine is to use the shutdown command.
There are two basic ways to use shutdown. If you halt the system, it shuts the machine down and keeps it down. To make the machine halt immediately, run this:
# shutdown -h now
On most machines and versions of Linux, a halt cuts the power to the machine. You can also reboot the machine. For a reboot, use -r instead of -h.
The shutdown process takes several seconds. You should avoid resetting or powering off a machine during a shutdown.
In the preceding example, now is the time to shut down. Including a time argument is mandatory, but there are many ways to specify it. For example, if you want the machine to shut down sometime in the future, you can use +n, where n is the number of minutes shutdown should wait before proceeding. See the shutdown(8) manual page for other options.
To make the system reboot in 10 minutes, enter:
# shutdown -r +10
On Linux, shutdown notifies anyone logged on that the machine is going down, but it does little real work. If you specify a time other than now, the shutdown command creates a file called /etc/nologin. When this file is present, the system prohibits logins by anyone except the superuser.
When the system shutdown time finally arrives, shutdown tells init to begin the shutdown process. On systemd, this means activating the shutdown units, and on System V init, it means changing the runlevel to 0 (halt) or 6 (reboot). Regardless of the init implementation or configuration, the procedure generally goes like this:
- init asks every process to shut down cleanly.
- If a process doesn’t respond after a while, init kills it, first trying a TERM signal.
- If the TERM signal doesn’t work, init uses the KILL signal on any stragglers.
- The system locks system files into place and makes other preparations for shutdown.
- The system unmounts all filesystems other than the root.
- The system remounts the root filesystem read-only.
- The system writes all buffered data out to the filesystem with the
syncprogram. p. 163 - The final step is to tell the kernel to reboot or stop with the
reboot(2)system call. This can be done by init or an auxiliary program, such asreboot,halt, orpoweroff.
The reboot and halt programs behave differently depending on how they’re called, which may cause confusion. By default, these programs call shutdown with the -r or -h options. However, if the system is already at a halt or reboot runlevel, the programs tell the kernel to shut itself off immediately. If you really want to shut down your machine in a hurry, regardless of any potential damage from a disorderly shutdown, use the -f (force) option.