Managing services in Ubuntu

There are 2 ways of using services in ubuntu:

 

  1. Traditional way: /etc/init.d/myservice start/stop/restart
  2. Upstart: service myservice start/stop/restart

Getting a list of Services

# Traditional:
ls /etc/init.d
# Upstart:
service --status-all

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.

Runlevels define what services or processes should be running on the system.

The init process can run the system in one of eight runlevels. The system runs only one of the eight runlevels at a time.

The main runlevels are from 0 – 6. Here’s what each runlevel is for:

Runlevel 0: Halt System – To shutdown the system
Runlevel 1: Single user mode
Runlevel 2: Basic multi user mode without NFS
Runlevel 3: Full multi user mode (text based)
Runlevel 4: unused
Runlevel 5: Multi user mode with Graphical User Interface
Runlevel 6: Reboot System

 Adding a Service to Default runlevels

# cp /etc/init.d/myscript.sh  << copy your script to /etc/init.d
# chmod +x /etc/init.d/myscript.sh
# update-rc.d myscript.sh defaults
update-rc.d: warning: /etc/init.d/myscript.sh missing LSB information
update-rc.d: see <http://wiki.debian.org/LSBInitScripts>
 Adding system startup for /etc/init.d/myscript.sh ...
   /etc/rc0.d/K20myscript.sh -> ../init.d/myscript.sh
   /etc/rc1.d/K20myscript.sh -> ../init.d/myscript.sh
   /etc/rc6.d/K20myscript.sh -> ../init.d/myscript.sh
   /etc/rc2.d/S20myscript.sh -> ../init.d/myscript.sh
   /etc/rc3.d/S20myscript.sh -> ../init.d/myscript.sh
   /etc/rc4.d/S20myscript.sh -> ../init.d/myscript.sh
   /etc/rc5.d/S20myscript.sh -> ../init.d/myscript.sh

Note:
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.

To make the script run with the start argument at the end of the start sequence, and run with the stop argument at the beginning of the shutdown sequence:

# sudo update-rc.d myscript defaults 98 02

98 and 02 are the start and stop sequence numbers respectively. Both are numbers between 00 and 99 and specify how early or late a service is started or killed. If a service is started late, it should be killed early and vice-versa. A good rule of thumb is to make the stop sequence number equal to 100 minus the start sequence number.

Removing a Service from Default runlevels

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

# Traditional - Something along the lines of
rm /etc/rc*/*myscript

# update-rc.d - This is the preferred way
update-rc.d -f apache2 remove

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

The advantage of using update-rc.d is that it will take care of removing/adding any required links to /etc/init.d automatically.


Sources:
How-To: Managing services with update-rc.d
Ubuntu Bootup Howto

You may also like