Ubuntu – how to auto start services on boot using update-rc.d (sysv init)

Utility update-rc.d is part of sysv init (package sysv-rc) on Ubuntu Linux. It can be used to manage services at boot time. It can be used to add or remove a service from auto start list. Note that this is only relevant for non upstart based services. Some upstart and sysv init based services at the time of writing this tutorial are:

  1. sysv init based: apache2, memcached, jenkin, etc. This tutorial is only relevant for sysv init based services.
  2. upstart based: mysql, docker, etc.

What does update-rc.d do?

update-rc.d creates symlinks in /etc/rc*.d/ for various run levels pointing to /etc/init.d/service_script (e.g. /etc/init.d/apache2) when a service is enabled to auto start on boot.

Add a service to auto start at boot time in dry-run mode

We’ll use nginx for this example. Also note that -n is for dryrun.

## dry-run
$ sudo update-rc.d -n nginx defaults
 Adding system startup for /etc/init.d/nginx ...
   /etc/rc0.d/K20nginx -> ../init.d/nginx
   /etc/rc1.d/K20nginx -> ../init.d/nginx
   /etc/rc6.d/K20nginx -> ../init.d/nginx
   /etc/rc2.d/S20nginx -> ../init.d/nginx
   /etc/rc3.d/S20nginx -> ../init.d/nginx
   /etc/rc4.d/S20nginx -> ../init.d/nginx
   /etc/rc5.d/S20nginx -> ../init.d/nginx

Add a service to auto start at boot time in real-run mode

We’ll use nginx for this example. Also note that we are not using -n here.

## real-run
$ sudo update-rc.d nginx defaults
 Adding system startup for /etc/init.d/nginx ...
   /etc/rc0.d/K20nginx -> ../init.d/nginx
   /etc/rc1.d/K20nginx -> ../init.d/nginx
   /etc/rc6.d/K20nginx -> ../init.d/nginx
   /etc/rc2.d/S20nginx -> ../init.d/nginx
   /etc/rc3.d/S20nginx -> ../init.d/nginx
   /etc/rc4.d/S20nginx -> ../init.d/nginx
   /etc/rc5.d/S20nginx -> ../init.d/nginx

See if a service will start at boot time

To so if service nginx will start at boot time we can use two approaches.

  1. By adding service to auto start in dry-run mode (-n). This will work if service has been added by update-rc.d.

    ## dry-run
    $ sudo update-rc.d -n  nginx defaults
     System start/stop links for /etc/init.d/nginx already exist.
    

    If we get “already exist” message, that means service will autostart at boot time.

  2. By viewing files in /etc/rc*.d/

    $ ls /etc/rc*.d/*nginx*
    /etc/rc0.d/K20nginx  /etc/rc1.d/K20nginx  /etc/rc2.d/S20nginx  /etc/rc3.d/S20nginx  /etc/rc4.d/S20nginx  /etc/rc5.d/S20nginx  /etc/rc6.d/K20nginx
    

    Presence of these script in /etc/rc*.d/* implies that nginx will autostart at reboot.

Remove a service from auto start at boot time in dry-run mode

We’ll use nginx for this example. Also note that -n is for dryrun. When service auto start is in enabled state, you will get the following outcome.

## dry-run
$ sudo update-rc.d -n -f nginx remove
 Removing any system startup links for /etc/init.d/nginx ...
   /etc/rc0.d/K20nginx
   /etc/rc1.d/K20nginx
   /etc/rc2.d/S20nginx
   /etc/rc3.d/S20nginx
   /etc/rc4.d/S20nginx
   /etc/rc5.d/S20nginx
   /etc/rc6.d/K20nginx

Remove a service from auto start at boot time in real-run mode

We’ll use nginx for this example. Also note that -n is not present here. When service auto start is setup, you will get the following outcome.

## real-run
$ sudo update-rc.d -f nginx remove
 Removing any system startup links for /etc/init.d/nginx ...
   /etc/rc0.d/K20nginx
   /etc/rc1.d/K20nginx
   /etc/rc2.d/S20nginx
   /etc/rc3.d/S20nginx
   /etc/rc4.d/S20nginx
   /etc/rc5.d/S20nginx
   /etc/rc6.d/K20nginx
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments