-3

Gilles wrote an excellent reply about how Linux kernel shuts down at https://unix.stackexchange.com/a/122667/674

I was wondering how Linux OS shuts down (in cases of both systemvinit and systemd)? I am expecting something similar to the boot sequence of Linux OS. I am particularly wondering how the processes notify each other, by some signal like SIGTERM and SIGKILL, or some other interprocess communication way?

Thanks.

Related:

What is the difference between "when the operating system shuts down" and "when the kernel shuts down"?

When the operating system shuts down, how does a service manager know that it should sends SIGTERM and SIGKILL to its services?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Tim
  • 101,790

1 Answers1

5

With both sysvinit and systemd, shutting the operating system down starts by notifying the init process (the process with pid 1) that the system should shut down (or reboot, or power off).

sysvinit does this by using the /run/initctl FIFO to communicate with init, asking it to switch to the corresponding runlevel. See the init manpage for a brief overview.

systemd supports a variety of ways to do this. Various signals can be used to request a shutdown, reboot, etc., in various flavours; it’s also possible to request this over d-bus (the busctl manpage explains how to explore that).

Once pid 1 has been asked to shut down, it follows its configuration and specification and goes through all the appropriate steps for it. This typically includes notifying all users that a shutdown is in progress, shutting down all the running services (in a managed way with systemd; using shutdown scripts in various forms with sysvinit), syncing the mounted file systems, possibly unmounting them, killing all remaining processes (with the usual TERM then KILL sequence), and finally calling the kernel’s reboot system call with the appropriate parameters.

This describes the general sequence. There are many more twists to all this, including access control (with Polkit), various available hooks, kexec, sudden power-off handling, CtrlAltDel handling... The systemd documentation covers many of these details.

Stephen Kitt
  • 434,908
  • Thanks. In https://manpages.debian.org/stretch/sysvinit-core/init.8.en.html#SIGNALS, init also reacts to some signals. Does that mean we can communicate with init by sending some signals instead of using FIFO, asking it to switch to the corresponding runlevel? – Tim Dec 19 '18 at 02:06
  • /run/initctl is FIFO not socket. Let me know if I am wrong. – Tim Dec 19 '18 at 03:43
  • You can use certain signals to communicate with sysvinit’s init, but none of them are relevant here. You can’t use them to request an arbitrary runlevel change. – Stephen Kitt Dec 19 '18 at 05:21