Debian/Ubuntu Tips and Tricks


Debian/Ubuntu Tips and Tricks

How-To: Managing services with update-rc.d

Posted by chantra on July 5th, 2007

Linux services can be started, stopped and reloaded with the use of scripts stocked in /etc/init.d/.

However, during start up or when changing runlevel, those scripts are searched in /etc/rcX.d/ where X is the runlevel number.

This tutorial will explain how one can activate, deactivate or modify a service start up.

When installing a new service under debian, the default is to enable it. So for instance, if you just installed apache2 package, after you installed it, apache service will be started and so will it be upon the next reboots.

If you do not use apache all the time, you might want to disable this service from starting up upon boot up and simply start it manually when you actually need it by running this command:

# /etc/init.d/apache2 start

You could either disable this service on boot up by removing any symbolic links in /etc/rcX.d/SYYapache2 or by using update-rc.d.

The advantage of using update-rc.d is that it will take care of removing/adding any required links to /etc/init.d automatically.
Taking apache2 as an example, let's examine how /etc/rcX.d is looking like:

# ls -l /etc/rc?.d/*apache2
lrwxrwxrwx 1 root root 17 2007-07-05 22:51 /etc/rc0.d/K91apache2 -> ../init.d/apache2
lrwxrwxrwx 1 root root 17 2007-07-05 22:51 /etc/rc1.d/K91apache2 -> ../init.d/apache2
lrwxrwxrwx 1 root root 17 2007-07-05 22:51 /etc/rc2.d/S91apache2 -> ../init.d/apache2
lrwxrwxrwx 1 root root 17 2007-07-05 22:51 /etc/rc3.d/S91apache2 -> ../init.d/apache2
lrwxrwxrwx 1 root root 17 2007-07-05 22:51 /etc/rc4.d/S91apache2 -> ../init.d/apache2
lrwxrwxrwx 1 root root 17 2007-07-05 22:51 /etc/rc5.d/S91apache2 -> ../init.d/apache2
lrwxrwxrwx 1 root root 17 2007-07-05 22:51 /etc/rc6.d/K91apache2 -> ../init.d/apache2

As you can see, for runlevels 0, 1 and 6 there is a K at the beginning of the link, for runlevels 2, 3, 4 and 5, there is a S. Those two letters stands for Kill and Start.
On Debian and Ubuntu, runlevels 2, 3, 4 and 5 are multi-users runlevels.
Runlevel 0 is Halt.
Runlevel 1 is single user mode
Runlevel 6 is reboot

1. Removing a Service

If you want to totally disable apache2 service by hand, you would need to delete every single link in /etc/rcX.d/. Using update-rc.d it is as simple as:

# update-rc.d -f apache2 remove

The use of -f is to force the removal of the symlinks even if there is still /etc/init.d/apache2.

Note: This command will only disable the service until next time the service is upgraded. If you want to make sure the service won't be re-enabled upon upgrade, you should also type the following:
# update-rc.d apache2 stop 80 0 1 2 3 4 5 6 .

2. Adding a service

2.1. Default priorities

Now, if you want to re-add this service to be started on boot up, you can simply use:

# update-rc.d apache2 defaults
Adding system startup for /etc/init.d/apache2 ...
/etc/rc0.d/K20apache2 -> ../init.d/apache2
/etc/rc1.d/K20apache2 -> ../init.d/apache2
/etc/rc6.d/K20apache2 -> ../init.d/apache2
/etc/rc2.d/S20apache2 -> ../init.d/apache2
/etc/rc3.d/S20apache2 -> ../init.d/apache2
/etc/rc4.d/S20apache2 -> ../init.d/apache2
/etc/rc5.d/S20apache2 -> ../init.d/apache2

2.2. Custom priorities

But as you can see, the default value is 20 which is pretty different than 91 ... a S20 link is started before a S91 and and K91 is kill before K20.
To force apache2 to be started with priorities 91 for both Start and Kill, we need to use the following command:

# update-rc.d apache2 defaults 91
Adding system startup for /etc/init.d/apache2 ...
/etc/rc0.d/K91apache2 -> ../init.d/apache2
/etc/rc1.d/K91apache2 -> ../init.d/apache2
/etc/rc6.d/K91apache2 -> ../init.d/apache2
/etc/rc2.d/S91apache2 -> ../init.d/apache2
/etc/rc3.d/S91apache2 -> ../init.d/apache2
/etc/rc4.d/S91apache2 -> ../init.d/apache2
/etc/rc5.d/S91apache2 -> ../init.d/apache2

2.3. Different priorities for Start and Kill

Alternatively, if you want to set different priorities for Start than for Kill, let say Start with 20 and Kill with 80, you will need to run:

# update-rc.d apache2 defaults 20 80
Adding system startup for /etc/init.d/apache2 ...
/etc/rc0.d/K80apache2 -> ../init.d/apache2
/etc/rc1.d/K80apache2 -> ../init.d/apache2
/etc/rc6.d/K80apache2 -> ../init.d/apache2
/etc/rc2.d/S20apache2 -> ../init.d/apache2
/etc/rc3.d/S20apache2 -> ../init.d/apache2
/etc/rc4.d/S20apache2 -> ../init.d/apache2
/etc/rc5.d/S20apache2 -> ../init.d/apache2

3. Specifying custom runlevels

Finally, if you only want to Start and Kill on specific runlevels, like for instance starting apache with priority 20 on runlevels 2, 3, 4 and 5 and Kill with priority 80 on runlevels 0, 1 and 6:

# update-rc.d apache2 start 20 2 3 4 5 . stop 80 0 1 6 .
Adding system startup for /etc/init.d/apache2 ...
/etc/rc0.d/K80apache2 -> ../init.d/apache2
/etc/rc1.d/K80apache2 -> ../init.d/apache2
/etc/rc6.d/K80apache2 -> ../init.d/apache2
/etc/rc2.d/S20apache2 -> ../init.d/apache2
/etc/rc3.d/S20apache2 -> ../init.d/apache2
/etc/rc4.d/S20apache2 -> ../init.d/apache2
/etc/rc5.d/S20apache2 -> ../init.d/apache2

Or, to start with priority 20 for runlevel 2, 3 and 4 and priority 30 for runlevel 5 and kill with priority 80 for runlevel 0, 1 and 6:

# update-rc.d apache2 start 20 2 3 4 . start 30 5 . stop 80 0 1 6 .
Adding system startup for /etc/init.d/apache2 ...
/etc/rc0.d/K80apache2 -> ../init.d/apache2
/etc/rc1.d/K80apache2 -> ../init.d/apache2
/etc/rc6.d/K80apache2 -> ../init.d/apache2
/etc/rc2.d/S20apache2 -> ../init.d/apache2
/etc/rc3.d/S20apache2 -> ../init.d/apache2
/etc/rc4.d/S20apache2 -> ../init.d/apache2
/etc/rc5.d/S30apache2 -> ../init.d/apache2

8 Responses to “How-To: Managing services with update-rc.d”

  1. It does not work :-(
    update-rc.d xyz defaults 20 80
    displays happily: using dependency based boot sequencing
    and is using priority 01 for all ignoring requested priorities 20 an 80.

  2. Great info, but I have a problem and need some guidance. I installed the transmission daemon as a service, but it showed up as a S01xxx file name each time in the rc.x folders. I have another service that I want to run before the transmission daemon, and that consistently appeared as S03xxx file names.

    Here's what I did:
    update-rc.d transmission-daemon defaults 50
    update-rc.d rootstartup defaults 10

    If I look at, for example, the rc2.d folder then this is what I see:

    From what I understand, the sequence will follow the file names, so the opposite startup order is happening. I've temporarily fixed this by renaming S01transmission-daemon to S05transmission-daemon, but I'm not happy that I had to do it that way.

    What am I missing or doing wrong?

  3. Hi, I've installed apache 2.4.6 from sources, and it runs manually via "sudo /usr/local/apache--v2.4.6/bin/apachectl -k start" ( and stops with ending "stop" ), and "It works!" shows on address localhost.

    Don't pay attention to such long weird path to apache, this is just for testing :)

    I can't get how to make apache to be started automatically ( with update-rc.d, I assume ), or, at least, manually with short command, like "apachectl -k start" ( message: "sudo: apachectl: command not found" ), "sudo service apache2 restart" ( message: "/usr/local/apache--v2.4.6: unrecognized service" ) or something like that.

    Tried "sudo update-rc.d /usr/local/apache--v2.4.6 default" and "sudo update-rc.d apache2 default"
    message: "update-rc.d: /etc/init.d//usr/local/apache--v2.4.6: file does not exist"

    with best regards

  4. Are you sure 'K' is for 'kill' and 'S' is for 'start'? Because on my default install of Ubuntu it has all the mounting and startup scripts running as K numbers and the umount and stop scripts are all running as 'S' numbers which is backwards from what you are indicating...

    For example on mine it's: S60umountroot and S90halt

    and: K20zfs-mount

  5. I just figured out now that on rc0.d and rc6.d run-levels, all of the entries whether K or S will still run the stop command regardless...

    So if some packages don't understand this, it might install conflicting symlinks as S's and others as the K's on these run-levels causing duplicates which can get messy...

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>