I’m running Ubuntu where I have the directories /etc/rc0.d, /etc/rc1.d, /etc/rc2.d, …, /etc/rc6.d.
Example files from my machine:
directory example symlinks in the dir ------------------------------------------ /etc/rc1.d: K76dovecot, K77ntp /etc/rc2.d: S23ntp, S24dovecot /etc/rc3.d: S23ntp, S24dovecot /etc/rc4.d: S23ntp, S24dovecot /etc/rc5.d: S23ntp, S24dovecot
Questions:
- What’s the purpose of the multiple “rc” directories?
- Why did Ubuntu install duplicates of
dovecotandntpinto all the directories exceptrc0.dandrc6.d? - If they are specified multiple times like above, are they actually executed multiple times?
- Can you tell from the above in what order
dovecotandntpwill execute at startup? - What is the proper way to tell Ubuntu to always execute
ntpbeforedovecotat startup?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
As others have noted, the answer is all about runlevels which are basically the modes of operation of an operating system. On Linux, these are usually:
ID Name Description
0 Halt Shuts down the system.
1 Single-user Mode Mode for administrative tasks.
2 Multi-user Mode Does not configure network interfaces and
does not export networks services.
3 Multi-user Mode with Networking Starts the system normally.
4 Not used/User-definable For special purposes.
5 Start the system normally with
with GUI As runlevel 3 + display manager.
6 Reboot Reboots the system.
So, each of the rcN directories contains symbolic links to the scripts that should be run at that runlevel. All the actual scripts are normally in the /etc/init.d directory:
$ ls -l /etc/rc5.d/S22cron lrwxrwxrwx 1 root root 14 Jan 14 2013 /etc/rc5.d/S22cron -> ../init.d/cron
Symbolic link naming
A symbolic link whose name begins with an S will be started at the runlevel in question while those whose name begins with K will be killed. Notice that all the links in rc6.d, the reboot runlevel, start with K. That’s because they should all be stopped for a reboot and nothing should be started.
The numbers after the initial letter refer to the running order of the linked scripts. Those with smaller numbers will be run before those with higher numbers. So, in your specific example, S23ntp will be run (started in this case) before S24dovecot.
Method 2
- These are
runlevels and are a System V-style initiation used by most *NIX systems (with the notable exception ofsystemd-based systems). When booting the kernel/user decides whatrunlevelshould it run and execute only thatrunlevel. Meaning that depending therunlevelyou can boot up with a different set of programs. There are runlevels for halt and reboot too, but since you are focusing on the startup part, let’s ignore them for now. - Since only one
runlevelis executed at boot, some programs should/want to start/stop at differentrunlevels with different or same parameters in the same or different order (not all runlevels are the same in all OS’s). But Ubuntu copy runlevels 3-5 from 2, that’s why they are the same. - No.
runlevels are executed just once in startup or when you changerunlevel. ntpscripts should execute first thendovecotin runlevel 2-5, not the case for runlevel 1. The ordinal number in the script names (S23ntp) states the order of execution. So, it all depends of the runlevel you are using.- It depends of the Distro but in the particular case of Ubuntu you can add your script to runlevel 1 and 2.
More info in the Wikipedia article about Ubuntu runlevels
Method 3
1) The multiple rcX.d directories specify what services to start or stop during the ‘X’ runlevel.
2) rc0.d is for runlevel 0 which is shutdown. rc6.d is for reboot. Rest all are for different runlevels (2 – 5). The S stands for start and K for Kill. These are essentially links to the original scripts in /etc/rc.d. The numbers after S/K are the priority by which the services will be started/Killed.
3) Yes if they are specified multiple times the start/kill script will be run multiple times. But no one wants to do that.
4) Looking at the priority numbers, ntp service will be started first followed by dovecot.
5) The 4th point’s the way.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0