26

I am learning command line from a book called "Linux Command Line and Shell Scripting Bible, Second Edition." The book states this:

Some Linux implementations contain a table of processes to start automatically on bootup. On Linux systems, this table is usually located in the special file /etc/inittabs.

Other systems (such as the popular Ubuntu Linux distribution) utilize the /etc/init.d folder, which contains scripts for starting and stopping individual applications at boot time. The scripts are started via entries under the /etc/rcX.d folders, where X is a run level.

Probably because I am new to linux, I did not understand what the second paragraph quoted meant. Can someone explain the same in a much plainer language?

3
  • 1
    this has been asked many times before
    – bsd
    Jan 9, 2012 at 16:30
  • 1
    @bdowning This one's different. I have a context. The questions I see in the page you linked to are different from this, IMO.
    – its_me
    Jan 9, 2012 at 16:41
  • 1
    @bdowning off, but flag as duplicate would have been more informative if you feel so
    – n611x007
    Jul 21, 2015 at 11:19

4 Answers 4

37

Let's forget init.d or rcx.d and keep things very simple. Imagine you were programming a program whose sole responsibility is to run or kill other scripts one by one.

However your next problem is to make sure they run in order. How would you perform that?

And lets imagine this program looked inside a scripts folder for running the scripts. To order the priority of scripts you would name them in lets say numerical order. This order is what dictates the relation between init.d and rc

In other words init.d contains the scripts to run and the rcX.d contains their order to run.

The X value in rcX.d is the run level. This could be loosely translated to the OS current state.

If you dig inside the rcX.d scripts you will find this formatting:

Xxxabcd
  • X is replaced with K or S, which stands for whether the script should be killed or started in the current run level
  • xx is the order number
  • abcd is the script name (the name is irrelevant however where it points is the script this will run)
1
  • 3
    wonderful explanation, very clear. Thanks @whoami
    – its_me
    Jan 10, 2012 at 13:15
12

There are several different init systems for Linux. The main ones are SysVinit (the traditional one), Upstart (Ubuntu's replacement), and SystemD (pushed by Fedora and Gnome). The directories /etc/init.d and /etc/rc?.d are used by SysVinit. The book may be mentioning them with regards to Ubuntu because the information is a bit dated (Ubuntu used to use SysVinit like everyone else) or because those directories still exist for compatibility.

/etc/init.d contains a bunch of scripts, each containing instructions to start and stop a service. Some of these services need to be started at boot time; others need to be started in multiuser mode but not in single-user maintenance mode; and it's possible to define different modes with different sets of desired services. SysVinit manages this through runlevels. The directory /etc/rc$N.d contain the scripts to run when entering runlevel N (/etc/rc$N.d/S*) and the scripts to run when leaving runlevel N (/etc/rc$N.d/K*). Because many runlevels have scripts in common, rather than store a copy of the scripts for each runlevel, the scripts are all stored in a single location /etc/init.d, and the runlevel-specific directories /etc/rc?.d contain symbolic links. Furthermore the names of the symbolic links indicate whether the service is to be started (S*) or stopped (killed, K*) in that runlevel, and a numeric prefix is used to control the order in which the scripts are executed.

The script responsible for traversing /etc/rc$N.d is /etc/init.d/rc (on pre-Upstart Ubuntu and on Debian; locations may vary on other Linux distributions).

3
  • So, Ubuntu is not using /etc/init.d and /etc/rc?.d anymore, and has Upstart instead?
    – its_me
    Jan 10, 2012 at 1:16
  • 1
    @KrishD'Souza Not quite. Ubuntu uses Upstart instead of SysVinit, but many packages still ship scripts in /etc/init.d (instead of Upstart .conf files in /etc/init), so Upstart still supports /etc/init.d and /etc/rc?.d for compatibility. Jan 10, 2012 at 1:21
  • Ubuntu switched from Upstart to systemd since 15.04. So at this moment (16.04) Ubuntu has three init systems: SysVinit, Upstart and systemd. First and second are left for backward compatibility.
    – Peter
    Jul 21, 2016 at 22:02
4

/etc/init.d is the directory where the init scripts belong.

The etc/rcX.d is where links controlling which services are killed or started when entering runlevel X. Files in rcX.d starting with K are executed with the parameter stop, and files starting with S are executed with the parameter start. It is typical to sequence startup and stop order using a two digit number after the K or S. To ensure proper start and stop ordering is it common for the two sequences to add up to 100.

Programs can be disabled in a run level by removing the links or changing the case of the K or S to k or s.

EDIT: Administrators typically run the scripts from /etc/init.d which may be a symbolic link to another directory depending on distribution. (Different distributions have different standards.)

The rcX.d are used by the initialization code to change run levels.

3
  • On most systems, /etc/init.d is a symlink to directory under /etc/rc.d/init.d Jan 9, 2012 at 16:41
  • 1
    So, basically /etc/init.d is useless without /etc/rcX.d? And since /etc/rcX.d is the controller of programs that are launched at startup, it's function is similar to that of /etc/init.d, correct?
    – its_me
    Jan 9, 2012 at 16:44
  • This depends on the system. Gentoo, for example, uses only /etc/init.d for it's init files; it has no /etc/rc*.d directories. Arch Linux, likewise, only has /etc/rc.d, instead of /etc/init.d.
    – laebshade
    Jan 10, 2012 at 0:41
1

Although @BillThor has anwered it very well, here's my understanding of /etc/rcX.d and /etc/init.d:

  • /etc/init.d contains scripts for starting and stopping individual applications at boot time.
  • /etc/rc?.d directories represent various run levels and the scripts contained within are nothing but symbolic links to the actual scripts in /etc/init.d directory. Changing the run levels changes the mode of the system, like the very basic Single user mode to console-only mode to the advanced graphical interface.

So, there's no use of /etc/rc?.d directories without the /etc/init.d itself (and of course, vice-versa).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .