| 1 |
|
| 2 |
Order of scripts run in /etc/rc?.d |
| 3 |
================================== |
| 4 |
|
| 5 |
0. Overview. |
| 6 |
|
| 7 |
All scripts executed by the init system are located in /etc/init.d. |
| 8 |
The directories /etc/rc?.d (? = S, 0 .. 6) contain relative links to |
| 9 |
those scripts. These links are named S<2-digit-number><original-name> |
| 10 |
or K<2-digit-number><original-name>. |
| 11 |
|
| 12 |
When ``init`` changes runlevel first the targets of the links whose |
| 13 |
names start with a ``K`` are executed, each with the single argument |
| 14 |
``stop``, followed by the scripts prefixed with an ``S``, each with the |
| 15 |
single argument ``start``. (The links are those in the ``/etc/rcn.d`` |
| 16 |
directory corresponding to the new runlevel.) The ``K`` links are |
| 17 |
responsible for killing services and the ``S`` link for starting |
| 18 |
services upon entering the runlevel. |
| 19 |
|
| 20 |
The two-digit number mm is used to determine the order in which to run |
| 21 |
the scripts: low-numbered links have their scripts run first. For |
| 22 |
example, the ``K20`` scripts will be executed before the ``K30`` |
| 23 |
scripts. This is used when a certain service must be started before |
| 24 |
another. For example, the name server ``bind`` might need to be started |
| 25 |
before the news server ``inn`` so that ``inn`` can set up its access |
| 26 |
lists. In this case, the script that starts ``bind`` would have a lower |
| 27 |
number than the script that starts ``inn`` so that it runs first: |
| 28 |
|
| 29 |
The two runlevels 0 (halt) and 6 (reboot) are slightly different. In |
| 30 |
these runlevels, the links with an ``S`` prefix are still called after |
| 31 |
those with a ``K`` prefix, but they too are called with the single |
| 32 |
argument ``stop``. |
| 33 |
|
| 34 |
If a scripts has the ".sh" suffix it is a bourne shell script and |
| 35 |
MAY be handled in an optimized manner. The behaviour of executing the |
| 36 |
script in an optimized way will not differ in any way from it being |
| 37 |
forked and executed in the regular way. |
| 38 |
|
| 39 |
The following runlevels are defined: |
| 40 |
|
| 41 |
N System bootup (NONE). |
| 42 |
S Single user mode (not to be switched to directly) |
| 43 |
0 halt |
| 44 |
1 single user mode |
| 45 |
2 .. 5 multi user mode |
| 46 |
6 reboot |
| 47 |
|
| 48 |
1. Boot. |
| 49 |
|
| 50 |
When the systems boots, the /etc/init.d/rcS script is executed. It |
| 51 |
in turn executes all the S* scripts in /etc/rcS.d in alphabetical |
| 52 |
(and thus numerical) order. The first argument passed to the |
| 53 |
executed scripts is "start". The runlevel at this point is "N" (none). |
| 54 |
|
| 55 |
Only things that need to be run once to get the system in a consistent |
| 56 |
state are to be run. The rcS.d directory is NOT meant to replace rc.local. |
| 57 |
One should not start daemons in this runlevel unless absolutely |
| 58 |
necessary. Eg, NFS might need the portmapper, so it is OK to start it |
| 59 |
early in the bootprocess. But this is not the time to start the |
| 60 |
squid proxy server. |
| 61 |
|
| 62 |
2. Going multiuser. |
| 63 |
|
| 64 |
After the rcS.d scripts have been executed, init switches to the |
| 65 |
default runlevel as specified in /etc/inittab, usually "2". |
| 66 |
|
| 67 |
Init then executes the /etc/init.d/rc script which takes care of |
| 68 |
starting the services in /etc/rc2.d. |
| 69 |
|
| 70 |
Because the previous runlevel is "N" (none) the /etc/rc2.d/KXXxxxx |
| 71 |
scripts will NOT be executed - there is nothing to stop yet, |
| 72 |
the system is busy coming up. |
| 73 |
|
| 74 |
If for example there is a service that wants to run in runlevel 4 |
| 75 |
and ONLY in that level, it will place a KXXxxxx script in |
| 76 |
/etc/rc{2,3,5}.d to stop the service when switching out of runlevel 4. |
| 77 |
We do not need to run that script at this point. |
| 78 |
|
| 79 |
The /etc.rc2.d/SXXxxxx scripts will be executed in alphabetical |
| 80 |
order, with the first argument set to "start". |
| 81 |
|
| 82 |
3. Switching runlevels. |
| 83 |
|
| 84 |
When one switches from (for example) runlevel 2 to runlevel 3, |
| 85 |
/etc/init.d/rc will first execute in alphabetical order all K |
| 86 |
scripts for runlevel 3 (/etc/rc3.d/KXXxxxx) with as first argument |
| 87 |
"stop" and then all S scripts for runlevel 3 (/etc/rc3.d/SXXxxxx) |
| 88 |
with as first argument "start". |
| 89 |
|
| 90 |
As an optimization, a check is made for each "service" to see if |
| 91 |
it was already running in the previous runlevel. If it was, and there |
| 92 |
is no K (stop) script present for it in the new runlevel, there is |
| 93 |
no need to start it a second time so that will not be done. |
| 94 |
|
| 95 |
On the other hand, if there was a K script present, it is assumed the |
| 96 |
service was stopped on purpose first and so needs to be restarted. |
| 97 |
|
| 98 |
We MIGHT make the same optimization for stop scripts as well- |
| 99 |
if no S script was present in the previous runlevel, we can assume |
| 100 |
that service was not running and we don't need to stop it either. |
| 101 |
In that case we can remove the "coming from level N" special case |
| 102 |
mentioned above in 2). But right now that has not been implemented. |
| 103 |
|
| 104 |
4. Single user mode. |
| 105 |
|
| 106 |
Switching to single user mode is done by switching to runlevel 1. |
| 107 |
That will cause all services to be stopped (assuming they all have |
| 108 |
a K script in /etc/rc1.d). The runlevel 1 scripts will then switch |
| 109 |
to runlevel "S" which has no scripts - all it does is spawn |
| 110 |
a shell directly on /dev/console for maintenance. |
| 111 |
|
| 112 |
5. Halt/reboot |
| 113 |
|
| 114 |
Going to runlevel 0 or 6 will cause the system to be halted or rebooted, |
| 115 |
respectively. For example, if we go to runlevel 6 (reboot) first |
| 116 |
all /etc/rc6.d/KXXxxxx scripts will be executed alphabetically with |
| 117 |
"stop" as the first argument. |
| 118 |
|
| 119 |
Then the /etc/rc6.d/SXXxxxx scripts will be executed alphabetically |
| 120 |
with "stop" as the first argument as well. The reason is that there |
| 121 |
is nothing to start anymore at this point - all scripts that are |
| 122 |
run are meant to bring the system down. |
| 123 |
|
| 124 |
In the future, the /etc/rc6.d/SXXxxxx scripts MIGHT be moved to |
| 125 |
/etc/rc6.d/K1XXxxxx for clarity. |