Through the years, machines have refined to the point of performing and synchronizing billions of sensitive but complex actions per second. Because of this, how we switch off any operating system (OS) without losing or corrupting data can be a critical consideration.
In this tutorial, we learn about ways to bring a Linux system down. First, we talk about the usual Linux shutdown process. Next, we go through the standard stop and restart commands, some of their historical background, and contemporary functions. Finally, we explore how the methods we discuss got centralized into a single binary.
We tested the code in this tutorial on Debian 11 (Bullseye) with GNU Bash 5.1.4. It should work in most POSIX-compliant environments.
2. Linux Shutdown Process
Of course, what goes up, must come down. If there is a Linux boot process, we also need steps to stop.
Basically, a shutdown means that the OS gives up control of the hardware. However, before doing that, an OS usually performs maintenance, which ensures several things:
- ties to devices such as file handles and block special files are gracefully severed
- all software is in a proper state
- each hardware component is in the proper state
- next boots are stable
In fact, abrupt stops may damage the filesystem or even the circuitry and hardware, depending on its exact nature.
For this reason, Linux usually goes through at least a minimal set of operations before giving up control of the hardware:
- Warn users
- Perform scheduled tasks
- Sync and unmount storage filesystems
- Stop system services
- Detach and power off devices
- Unload kernel modules
- Move through and stop targets (run levels)
- Stop and prevent user sessions
- Disable terminal services
- Disable security mechanisms
- Disable logging
While the order is more or less as listed, not all distributions and installations follow the exact same process, so a step can get delayed, omitted, or intertwined.
Perhaps the most critical steps relate to the sensitive structure of storage components. Leaving at least the OS configuration in a coherent state while unmounting filesystems is paramount.
So, with our basic understanding of the shutdown process Linux follows, let’s explore some commands to initiate it.
The halt command simply stops the OS without consideration for the hardware state after that.
To understand the idea behind halt, we turn to the Advanced Configuration and Power Interface (ACPI), which provides the OS with the means to power off attached devices:
- storage (controllers)
- processor, i.e., central processing unit (CPU)
- main memory, i.e., random access memory (RAM)
On older or specialty machines without a convenient ACPI-controlled power button, halt usually performs the necessary stop procedures for everything the OS has control over but doesn’t power off anything. Essentially, this leaves the machine running but without third-party OS control. At that point, an operator can physically switch the power off.
Critically, there are very old versions of halt that don’t go through a graceful stop process, instead directly literally halting the OS.
Due to the advent and widespread use of ACPI, halt now usually incorporates the protocol, becoming very similar, if not equivalent to the next Linux command.
Indeed, the poweroff command was initially just an ACPI call to directly power off the machine without any shutdown procedure. In other words, it was the equivalent of holding down the power button or pulling the plug.
However, due to the aforementioned repercussions of simply switching off a complex system of hardware and software, this theoretical complement to halt came to include it. Similarly, contemporary versions of halt include the poweroff ACPI call, if supported.
Consequently, the two commands are usually pretty much equivalent.
The function of reboot is to bring the system down and then bring it back up.
How it does that usually depends on the options. Before ACPI, reboot just did a halt and restart without power operations. Contemporary versions usually go through the whole process:
- graceful stop
- power off
- power on
- graceful start
Because of the merge between halt and poweroff, as well as the fact that reboot combines them, all three commands are commonly symbolic links to the same binary.
Since, in the past, halt, poweroff, and reboot were distinct options for stopping, shutdown was meant to further combine maintenance, termination, and restarts into one:
- perform the proper tasks to gracefully stop the OS
- power off the system
- optionally power the system back on
Whether this is done by calling them in order or via a custom binary depends on the version and implementation.
Since shutdown includes all previous commands, we can often use it to call them via flags:
- -H or –halt for halt
- -P or –poweroff for poweroff
- -R or –reboot for reboot
After exploring the historic commands for stopping and restarting a system, let’s bring them all together.
7. OS Stop Control
At this point, the names of stop commands can just be semantically different, while the underlying code paths remain basically the same. Consequently, we can have unexpected combinations:
$ halt --poweroff
$ poweroff --reboot
$ reboot --halt
Critically, the specific actions of each command depend on many factors:
- philosophy of the developer or distribution
- init system
Some environments merge halt and reboot in the same binary, while shutdown and poweroff are a different binary. Others combine everything into one.
In addition, there is usually a -f, -ff, or –force switch, which, like the original halt, doesn’t contact the system manager further before performing any stop operations. This means we jump over the shutdown functionality and any graceful stops, risking system damage but ensuring the action completes as quickly as possible.
Due to all of the above, referring to the documentation of a UNIX or Linux distribution can be vital to how the options we have for system state control are implemented.
In this article, we looked at the usual commands for stopping and restarting a Linux system, some of their history, and their current status quo.
In conclusion, while there were historical differences between halt, poweroff, and shutdown based on technicalities, their contemporary codebase is much more unified, if not equivalent, depending on the system.