2010-05-26

Ubuntu service management

Once upon a time, service management in Ubuntu, like other Debian derivates, was easy. Now, in the days of 10.04 recently released, I find it confusing to the point where I felt the need to write down some notes on the subject.

I'm a control freak, but as the years fly by, I'm also getting increasingly lazy and obsessed with getting things done efficiently, and not manually by hand for stuff I do often. I started off with Slackware and Gentoo, but during the latest years working with VoIP, I'ved used Ubuntu more and more of the time. So, I've learned to love Ubuntu for how easy it is, and how quickly I can go from installing it on a machine to working efficiently with it, but also hate what Ubuntu does different from more manual distros like Arch, and the two aforementioned, when it comes to easily deciding for yourself what to enable or disable.

As an example; I do some web development now and then, but it's far from often. So I want MySQL, Apache2 and PHP installed, but I don't want MySQL and Apache2 to start other than if I choose to start them manually myself.
As Ubuntu sets them to start automatically at boot, in a different way for each of them, you also need to do it in different ways for both of them in order to disable them.

First look in /etc/init.d/ to see the installed services and their init-scripts. You should see that some of them are scripts, and some are symlinks. Those that are actual files/scripts, are the old-style sysvinit scripts, and you enable/disable them with update-rc.d. This means Apache2 can be disabled by typing:

# update-rc.d -f apache2 remove

as root, which will remove all symlinks in the /etc/rc?.d/ which in effect will make the init scripts to not pick up on starting them automatically, and so they are disabled.


With the new style upstart init scripts, which is event-based, things are done differently, and that's the case with MySQL. A listing of /etc/init.d/ will show you that mysql is actually just a symlink to /lib/init/upstart-job. The upstart-job script does some common initialisation, and looks in /etc/init/ for files ending in .conf, and reads those files, which specify which events that may trigger the startup of the service, and some other details. If you edit the file corresponding to the service you wish to disable, like in this case, /etc/init/mysql.conf, you'll find some lines in the beginning of the file, looking like this:


# MySQL Service

description     "MySQL Server"
author          "Mario Limonciello "

start on (net-device-up
          and local-filesystems)
stop on runlevel [016]

respawn
...


The parameters to "start_on" define which events will trigger this service to start. If you specify the event "never", which will never be triggered, the effect is, due to the expression being AND-ed, that the evaluation always returns false, and the service can only be started by calling it through "service ". So, if you modify the mysql.conf file to look like this in the beginning:


# MySQL Service

description     "MySQL Server"
author          "Mario Limonciello "

start on (never
          and net-device-up
          and local-filesystems)
stop on runlevel [016]

respawn
...


the result is that the service can only be started manually, which is just how I want it.

So, what I do now, when I need to start or stop the services, is (as root):


# /etc/init.d/apache2 start|stop|restart
# service mysql start|stop|restart


Hope this can be of use to someone, or at least myself, if I should forget how to. Enjoy deciding for youself, and not letting Canonical/Ubuntu make all choices for you!