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:

  1. What's the purpose of the multiple "rc" directories?
  2. Why did Ubuntu install duplicates of dovecot and ntp into all the directories except rc0.d and rc6.d?
  3. If they are specified multiple times like above, are they actually executed multiple times?
  4. Can you tell from the above in what order dovecot and ntp will execute at startup?
  5. What is the proper way to tell Ubuntu to always execute ntp before dovecot at startup?
up vote 22 down vote accepted
  1. These are runlevels and are a System V-style initiation used by most *NIX systems (with the notable exception of systemd-based systems). When booting the kernel/user decides what runlevel should it run and execute only that runlevel. Meaning that depending the runlevel you 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.
  2. Since only one runlevel is executed at boot, some programs should/want to start/stop at different runlevels 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.
  3. No. runlevels are executed just once in startup or when you change runlevel.
  4. ntp scripts should execute first then dovecot in 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.
  5. 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

  • so if I put script to all RC.X dirs, it will execute few times or not? – Flash Thunder May 13 '16 at 12:22
  • @FlashThunder no. – Braiam May 13 '16 at 14:27

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 rcX directories contains the scripts that should be run at the different runlevels. These scripts are not installed multiple times, each entry under one of the rcXs is just a link to the real script which is 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

Now, the naming scheme is also quite simple. Scripts 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 scripts in rc6.d, the reboot runlevel, start with K. That's because they should all be stoped for a reboot and nothing should be started. The numbers refer to the running order of the 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.

  • Great answer! What if two scripts have the same numbers, for example: S01bluetooth and S01rsync, will the order be arbitrary? – direprobs Sep 15 '17 at 20:48
  • @direprobs huh, good question. Yes, I would guess that either one would be chosen randomly. Can't say that I know, though. – terdon Sep 15 '17 at 21:59
  • @tredon I think they'll be chosen based on their filenames since they begin with S01 the comparison will be b < r, you get the idea. Maybe! – direprobs Sep 15 '17 at 22:05
  • 1
    The short answer is, /etc/init.d/rc says for s in /etc/rc$runlevel/S*; do …, so it simply goes in glob expansion order; i.e., numeric / alphabetic. The long answer is that the code in there is a lot more complicated than I remember.  I'll try to spend some serious time studying it sometime within the next six to eight weeks.  At first glance, it looks like it's extracting the number after the S and doing something with it, so S42beeblebrox and S42zaphod might be more tightly associated than, say, S43arthur and S44dent. – G-Man Sep 15 '17 at 22:39
  • really thanks for great answer,But I have a question. I put my program in startup, but the speed of app is low:stackoverflow.com/questions/49251955/… – H.Ghassami Mar 13 at 10:24

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.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.