When the system transitions from one runlevel to another, it executes all the "K" scripts in the existing runlevel, in order, then all the "S" scripts in the new runlevel, in order. Debian implements parallel boot, so most of the scripts will run concurrently (but effectively in order), and there's a standard for including dependency information on each script, and tools for inserting/removing scripts. You can turn concurrency off by adding
On Debian's current
inittab, the command set to run at each runlevel transition is
When you boot, the system is considered in runlevel
S. The "S" scripts in
/etc/rcS.d are executed, in order (by
When they complete, the system is moved into the runlevel you specified on the kernel command line, or the default runlevel, which is 2 if you don't have X installed.
There are no "K" scripts in
/etc/rcS.d so nothing happens there - runlevel "S" is meant for general system initialization. But then all the "S" scripts in
/etc/rc2.d are executed, in order.
Once those complete, you are officially in runlevel 2. The
/etc/init.d/rc command returns, and
init spaws the
getty's it's configured to do, and waits for a signal to change runlevel.
When you shutdown, a signal is sent to
init. Then, the system is moving from runlevel 2 to 6 (reboot) or 0 (shutdown). So all the "K" scripts in
/etc/rc2.d are executed, in order. Then, all the "S" scripts in either
/etc/rc0.d are executed, in order, if they exist.
Then since, this is runlevel 0 or 6 (ook at
/etc/init.d/rc - that script explicitly tests for 0 or 6), it will immediately transition out of the runlevel, causing all the "K" scripts to run.
The final "K" script for 0 or 6 will be a command that halts/powers off or reboots the system.