Ubuntu startup – init scripts, runlevels, upstart jobs explained
Ubuntu 10.04 Lucid Lynx; believed to be relevant for Ubuntu 8.x to 11.x, the latest release at the time of writing this article
Ubuntu has 2 different mechanisms for starting system services:
- The traditional mechanism based on run levels, and scripts in /etc/init.d and /etc/rcn.d directories
- A new mechanism known as upstart.
Some services are started using one mechanism and others using the other. If you want to control the services, it’s necessary to understand these mechanisms.
Run levels and init.d scripts – the traditional mechanism
Linux has the concept of run levels, in all distros as part of the Linux Base Specification. They can be considered to be “modes” in which Linux runs.
|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 GUI display manager||Run level 3 + display manager|
|6||Reboot||Reboots the system|
|s or S||Single-user mode||Does not configure network interfaces, or start daemons.|
In Ubuntu (and Debian), run levels 2 to 5 are equivalent and configured with the same set of services.
Get Current run level
Use the runlevel command to get current run level. runlevel is available in Ubuntu as well as redhat based distros like CentOS (not sure about other distros).
The /etc/init.d directory contains scripts, which can start / stop / restart services. These are invoked with a start|stop argument at startup and shutdown.
The /etc/rcn.d directories specify which scripts in /etc/init.d are enabled for run level n.
For example, /etc/rc2.d specifies which scripts in /etc/init.d are enabled for run level 2. At startup and shutdown, only these enabled scripts are invoked.
Entries in /etc/rcn.d directories are symlinks to scripts in /etc/init.d, but with a special prefix of the format
S means the script is enabled for this run level.
K means the script is disabled for this run level.
nn is a sequence number that can be used to control the sequence of starting services, so that services which depend on other services are started only after those other services are started.
Below is a listing or /etc/rc2.d. It shows that tomcat6, dovecot and postfix are not automatically started in run level 2. However, they can be started manually.
K08tomcat6 K76dovecot K80postfix S20gpm S20winbind S50rsync S70dns-clean S70pppd-dns S91apache2 S99grub-common S99ondemand S99rc.local
Enabling and disabling run level services
Use the chkconfig –list command to get an overview of all services and their status. If not installed, install it using sudo apt-get install chkconfig. It gives a status listing like this:
karthik@ubuntukarmic:~$ chkconfig --list acpi-support 0:off 1:off 2:on 3:on 4:on 5:on 6:off acpid 0:off 1:off 2:off 3:off 4:off 5:off 6:off alsa-utils 0:off 1:off 2:off 3:off 4:off 5:off 6:off ...
Use the update-rc.d command to enable or disable a service at a run level:
Syntax: sudo update-rc.d name enable|disable runlevel
Example: sudo update-rc.d dovecot disable 2
sudo update-rc.d dovecot defaults
When creating new init scripts, ensure that the script has the following section (this is an example – change values appropriately) at the top to make it LSB (Linux Standard Base) compliant. Without this section, update-rc.d won’t work but will give a “missing LSB information” warning…
### BEGIN INIT INFO # Provides: solr # Required-Start: $local_fs $remote_fs $network # Required-Stop: $local_fs $remote_fs $network # Should-Start: $named # Should-Stop: $named # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Start Solr. # Description: Start the solr search engine. ### END INIT INFO
Upstart jobs are configured in /etc/init directory, in .conf files.
Use the service command to start and stop upstart services:
sudo service <servicename> start|stop
For disabling an upstart service from starting up, open the respective /etc/init/[service].conf file and comment out the lines that begin with start on.
... #start on (net-device-up # and local-filesystems # and runlevel ) ...
This will disable the service from starting at startup, but allow manual starts using service start command.
For completely disabling a service – both from automatic and manual starts – it’s better to uninstall the package, but it’s also possible to just rename the .conf file to .conf.disabled.
Resources for further reading
- http://askubuntu.com/questions/19320/whats-the-recommend-way-to-enable-disable-services/20347#20347 – this post from an Ubuntu developer explains in detail the history behind the init.d mechanism, its problems, and how the new Upstart mechanism solves them.
- http://oldfield.wattle.id.au/luv/boot.html – The Linux boot process.
- http://upstart.ubuntu.com/cookbook/#what-is-upstart – From the Upstart Cookbook