How to enable and disable services manually

You did it the old fashioned way which was creating or deleting symbolic links in the respective /etc/rcX.d/ directories. Here X in rcX.d is a number which stands for the runlevel.

As an example, here is a listing of all the services started in runlevel 5 in my PC running Ubuntu.

ls -l /etc/rc5.d

And the output is …

lrwxrwxrwx 1 root root  26 2012-03-18 21:10 S20apache2 -> ../init.d/apache2
lrwxrwxrwx 1 root root  20 2011-10-27 12:46 S20kerneloops -> ../init.d/kerneloops
lrwxrwxrwx 1 root root  17 2012-03-19 13:09 S20postfix -> ../init.d/postfix
lrwxrwxrwx 1 root root  27 2011-10-27 12:46 S20speech-dispatcher -> ../init.d/speech-dispatcher
... Shortened for brevity ...
lrwxrwxrwx 1 root root  22 2011-10-27 12:46 S99acpi-support -> ../init.d/acpi-support
lrwxrwxrwx 1 root root  21 2011-10-27 12:46 S99grub-common -> ../init.d/grub-common
lrwxrwxrwx 1 root root  18 2011-10-27 12:46 S99ondemand -> ../init.d/ondemand
lrwxrwxrwx 1 root root  18 2011-10-27 12:46 S99rc.local -> ../init.d/rc.local

As you can see, all the content in the /etc/rc5.d directory are symbolic links pointing to the corresponding script in the /etc/init.d directory.

There can be two kinds of symbolic links in the /etc/rcX.d/ directories.

One starts with the character ‘S’ followed by a number between 0 and 99 to denote the priority, followed by the name of the service you want to enable.

The second kind of symlink has a name which starts with a ‘K’ followed by a number and then the name of the service you want to disable.

So in any /etc/rcX.d directory, at any given time, for each service, there should be only one symlink of the ‘S’ OR ‘K’ variety but not both.

So coming back to our Apache example, suppose I want to enable Apache web server in the run level 5 but want to disable it in all other run levels, I do the following:

First to enable the service for run level 5, I move into /etc/rc5.d/ directory and create a symlink to the apache service script residing in the /etc/init.d/ directory as follows:

# cd /etc/rc5.d/
# ln -s /etc/init.d/apache2 S20apache2

This creates a symbolic link in the /etc/rc5.d/ directory which the system interprets as – start (S) the apache service before all the services which have a priority number greater than 20.

If you do a long listing of the directory /etc/rc5.d in your system, you can find a lot of symlinks similar to the one below.

# ls -l /etc/rc5.d |grep apache2
lrwxrwxrwx 1 root root  26 2012-03-18 21:10 S20apache2 -> ../init.d/apache2

Now if I start a service, I will want to stop the service while rebooting or while moving to single user mode and so on. So in those run levels I have to create the symlinks starting with character ‘K’.

So going back to the apache2 service example, if I want to automatically stop the service when the system goes into run level 0, 1 or 6, I will have to create the symlinks in the /etc/rc0.d, /etc/rc1.d/, /etc/rc6.d/ directories as follows –

# cd /etc/rc0.d
# ln -s /etc/init.d/apache2 K80apache2

One interesting aspect here is the priority. Lower the number, the higher is the priority.

So since the starting priority of apache2 is 20 – that is apache starts way ahead of other services during startup, we give it a stopping priority of 80.

There is no hard and fast rule for this but usually, you follow the formula as follows:

If you have ‘N’ as the priority number for starting a service, you use the number (100-N) for the stopping priority number and vice versa.