Linux › Section F · How user space starts › Topic F.5

Shutting down your system

Turning a Linux machine off is a process, not a switch. A proper shutdown lets services finish their work, writes buffered data to disk and unmounts filesystems cleanly, so the next boot starts from a consistent state.

Why the power button is not enough

A running system keeps a great deal in memory: file data waiting to be written, database transactions in progress, and services holding state they would like to save. Cutting the power loses all of it. Journaling filesystems usually recover their own structure afterwards, but they cannot recover data that never reached the disk, and files being written at that moment can be left damaged. A clean shutdown avoids this. init, whichever implementation it is, controls the shutdown, and the commands you type are the same on most systems. Only the root user, or a user permitted through sudo or, for someone at a local desktop, through polkit policy, may shut a machine down.

The shutdown command

On a systemd machine the syntax is shutdown [OPTIONS] [TIME] [WALL], where WALL is an optional message sent to every logged-in user. With no option, shutdown powers the machine off; -P says so explicitly and -h means the same. Use -r to reboot, or -H to halt, which stops the operating system but may leave the hardware powered on. TIME can be now, which means +0; +m, meaning m minutes from now; or hh:mm on the 24-hour clock. If you give no time, systemd's shutdown waits one minute. You must give a time if you want to add a message. shutdown -c cancels a pending shutdown, and shutdown --show displays it.

Worked example: a scheduled reboot

It is 21:40 and you need to reboot a shared server at 22:30 for a kernel update. 1. Run sudo shutdown -r 22:30 "Reboot for kernel update, back by 22:40". 2. Logged-in users receive the message as a wall broadcast, repeated as the time approaches. 3. At 22:25, five minutes before, systemd creates /run/nologin; while it exists, the login system refuses new logins from anyone but root. 4. sudo shutdown --show confirms the pending reboot and its time. 5. If plans change, sudo shutdown -c cancels it and lets users log in again. 6. Check: had you typed 21:30 by mistake, a time already past today, shutdown would not report an error; it would reboot immediately.

systemctl and the classic commands

On systemd, the direct commands are systemctl poweroff, systemctl reboot and systemctl halt, plus systemctl suspend and systemctl hibernate for sleep states. The traditional names reboot, poweroff and halt still work; systemd provides them as compatibility commands that ask it to do the same thing. Be careful with halt: on many older systems it powered the machine off, but under systemd it only halts, leaving the power on. shutdown is the better choice when you need a delay or a warning, and the systemctl verbs when you want to act now. Like any other request to systemd, each of these becomes a job that activates a target such as reboot.target or poweroff.target.

What systemd does during shutdown

Activating poweroff.target or reboot.target pulls in shutdown.target, which conflicts with ordinary services, so systemd stops them, in reverse of their start order where After= links exist. Each service first runs its ExecStop= command if it has one, and any of its processes still left receive SIGTERM. If it is still running when its stop timeout ends (TimeoutStopSec=, which defaults to 90 seconds upstream), systemd sends SIGKILL. Next, umount.target unmounts filesystems and deactivates swap. Finally the binary systemd-shutdown replaces PID 1. It kills any remaining processes, unmounts what it can and remounts the rest read-only, turns off swap, detaches storage devices, runs any programs in /usr/lib/systemd/system-shutdown/, and asks the kernel to power off or reboot.

The general sequence

Whatever the init system, a clean shutdown follows the same broad steps. 1. Services are asked to stop cleanly. 2. Any that ignore the request receive SIGTERM, and stragglers are then killed with SIGKILL; "Listing and manipulating processes: signals and jobs" covers those signals. 3. Filesystems other than the root are unmounted. 4. The root filesystem is remounted read-only, so nothing more can change on it. 5. Buffered data is flushed to disk. 6. init, or a helper, makes the reboot system call, documented as reboot(2), to halt, power off or restart. On System V init, shutdown achieved this by switching to runlevel 0 to halt or 6 to reboot, whose K scripts stopped each service.

Worked example: a shutdown that hangs

A server takes ages to reboot, showing a message such as "A stop job is running for report.service" with a countdown. 1. After it comes back, read the previous boot's log with journalctl -b -1 -u report.service. This needs a persistent journal, as "System logging: the journal and syslog" explains. 2. The log shows the service ignoring SIGTERM until it is killed after 90 seconds. 3. The real fix is in the program: it should exit promptly on SIGTERM. 4. Meanwhile, sudo systemctl edit report.service and add [Service] with TimeoutStopSec=15s, so systemd escalates to SIGKILL sooner. 5. Check: the next reboot waits at most about 15 seconds for that service.

Forcing the issue, and its risks

Sometimes a clean shutdown is impossible because a service or the system manager itself has stopped responding. systemctl reboot --force skips stopping services but still kills processes and unmounts or remounts filesystems read-only, so data on disk stays consistent. Giving --force twice reboots immediately, without killing processes or unmounting anything, which is about as risky as pressing the reset button; reboot -f is mostly equivalent to that double force. Warning: use these only when the normal route has failed, and run sync first if you can. Never force a reboot during a firmware or kernel update, because an interrupted write can leave the machine unbootable.

Common mistakes to avoid

First, cutting power or holding the power button as a habit, which risks losing buffered data. Second, typing an hh:mm time that has already passed, which shuts down immediately instead of tonight. Third, rebooting a shared server without a delay and a wall message, so users lose their work. Fourth, using halt and expecting the machine to power off; under systemd it stays on. Fifth, raising TimeoutStopSec= to hide a service that ignores SIGTERM instead of fixing it. Sixth, reaching for --force --force or reboot -f before trying a normal reboot. Seventh, running shutdown on the wrong host over SSH; check the prompt's host name first.

Key takeaways

Shut down through init so services stop, buffers are flushed and filesystems are unmounted. shutdown powers off by default; -r reboots, -H halts and -c cancels, with TIME given as now, +m or hh:mm, and +1 assumed if omitted. A delayed shutdown sends wall messages and creates /run/nologin five minutes before. systemctl poweroff, reboot, halt and suspend act at once; under systemd, halt leaves the power on. systemd stops services with ExecStop= or SIGTERM, then SIGKILL after TimeoutStopSec= (90 s by default), unmounts filesystems, and hands over to systemd-shutdown, which calls reboot(2). --force skips service shutdown; a double force or reboot -f is as risky as a reset.

Flashcards

Flashcard

shutdown -r now

Reboot immediately

now is the same as +0.

Test your understanding

Question 1 of 5

At 14:00 an administrator runs sudo shutdown -r 09:00, meaning tomorrow morning. What happens on a systemd system?

Question 2 of 5

Which command reboots in 10 minutes and warns users first?

Question 3 of 5

During a systemd shutdown, a service ignores SIGTERM. What does systemd do next?

Question 4 of 5

Five minutes before a scheduled shutdown, a regular user cannot log in. Why?

Question 5 of 5

Which step comes last in a clean shutdown?

← Previous topicNext topic →