I read about how to enable and disable services in Ubuntu and it seems that there are different possibilities manage theme?

The first method i found is update-rc.d to add new services to startup, which aims on the /etc/init.d folder and it's contents.

The other one i found is the way to edit .conf the files in /etc/init folder.

What's the recommended way to enable / disable / add services and why?

Could you please give a short bulletproof step by step example how to add a service xyz in Ubuntu and enable and disable it?

link|improve this question

5  
This is now documented in the paragraph Disabling a Job from Automatically Starting. – Marcello Nuccio Aug 20 '11 at 17:12
feedback

3 Answers

up vote 12 down vote accepted

I can show you two ways. There are services that can be enable/disable using the GUI (Like Gnome) and the other is using the terminal.

For the GUI

  1. Go to System -> Preferences -> Startup Applications

  2. Here you can click and enable/disable SOME of the services that start from a Desktop Ubuntu. Simply click on one.

For the Terminal you have MANY options. Like the ones you just mentioned, here are 2 more

For a temp enable/disable solution you can

Type sudo /etc/init.d/SERVICE_NAME For example:

sudo /etc/init.d/apache2 stop (To DISABLE until Reboot or until you start it again)

sudo /etc/init.d/apache2 start (To ENABLE)

Or using service:

sudo service mysql start (TO Start)

sudo service mysql stop (TO Stop until Reboot or until you start it again)

link|improve this answer
1  
Just a note here, please see the documented solution at the top. As of writing on 11.04, the suggestion (here and in various other places) to use bum is invalid. Bum does not support upstart. Try it youself; sudo apt-get install bum -> disable gdm -> cat /etc/init/gdm.conf. – Doug Aug 22 '11 at 3:53
Updated to remove bum as mentioned by Doug. Tested already and yeap, does not work anymore. – Luis Alvarado Dec 12 '11 at 23:44
I've found that bum still does work for some services (11.10). I deactivated the tor service, and it didn't run on reboot. I reactivated it, and it ran on reboot. I think bum affects the old /etc/init.d services, but not upstart services? – Jay Apr 1 at 20:20
feedback

Currently there are actually 2 different ways for software to be started as a service in Ubuntu. A service is defined here as a program run by the system in the background, as opposed to one started and run directly by the user.

The traditional way to start services in Linux was to place a script in /etc/init.d, and then use the update-rc.d command (or in RedHat based distros, chkconfig) to enable/disable it. This command, btw, uses some mildly complicated logic to create symlinks in /etc/rc#.d, that control the order of starting services. If you run ls /etc/rc2.d you can see the order that services will be killed (K##xxxx) and started (S##xxxx).

The issue with that was that when booting the system, everything had to be done in serial, one thing after another, making system boot times really slow. Attempts were made to parallelize this, but they were haphazard and hard to take full advantage of. This was the main reason that Upstart was created.

Upstart uses job definition files in /etc/init to define on what events a service should be started. So, while the system is booting, upstart processes various events, and then can start multiple services in parallel. This allows them to fully utilize the resources of the system, for instance, by starting a disk-bound service up while another CPU-bound service runs, or while the network is waiting for a dynamic IP address to be assigned.

You can see all of the upstart job files by running ls /etc/init/*.conf

Let me just stop here and say that if you don't know what a service is, or what it does, DO NOT disable it!

Not all services have been converted to upstart. While working on the server team at Canonical for the past few months, I've worked on a number of converted job files, and the nicest part is that it allows one to get rid of all the script "magic" and just put in a few commands here and there to define exactly how to start the service, and nothing more. But for now, only a handful of traditional network services, like squid and samba, have been converted.

In order to figure out if a service is upstart based, you can run the status command:

status servicename

If its an upstart job, it will show this:

$ status statd
statd start/running, process 942

But if its not, you'll see something more like this:

$ status apache2
status: Unknown job: apache2

In this case, apache2 has not been converted to upstart. So, to disable apache2 you just run

sudo update-rc.d apache2 disable
sudo service apache2 stop

Upstart job definitions do not have an update-rc.d command. To disable the job, you need to edit the job file directly to disable it. There are two ways to do this.

If you want to still be able to manually start it, then you need to comment out the 'start on' condition. Say you want to install samba, but not have it start automatically.. here is its job file (in natty):

description "SMB/CIFS File Server"
author      "Steve Langasek <steve.langasek@ubuntu.com>"

start on local-filesystems
stop on runlevel [!2345]

respawn

pre-start script
    RUN_MODE="daemons"

    [ -r /etc/default/samba ] && . /etc/default/samba

    [ "$RUN_MODE" = inetd ] && { stop; exit 0; }

    install -o root -g root -m 755 -d /var/run/samba
end script

exec smbd -F

To disable it, you can just put a # in front of the 'start on local-filesystems'. Note that while it won't start back up on boot, you still need to stop it this time with

sudo service smbd stop

If, however, you never want it to start, I'd suggest actually removing the package. If, however, you want it installed, but not startable, you can also do:

mv /etc/init/smbd.conf /etc/init/smbd.conf.disabled

Starting with the version of upstart that will be in 11.04, there is a new keyword that disables the 'start on' and 'stop on' stanzas, it is 'manual'. So another way to disable the service as of 11.04 is to do:

command using sudo
echo 'manual' | sudo tee /etc/init/mysql.override

command from root shell
echo manual >> /etc/init/smbd.conf

And, hopefully real soon, you will be able to create an "override" file to disable a service without editing the job definition at all, by just putting the 'manual' keyword in it.

link|improve this answer
Great, thanks. Finally figured out how to stop a couple of "mystery services", and enough that I can start experimenting with services on my own. – j-g-faustus Jan 6 '11 at 21:20
6  
Looks like 11.04 has override too. So echo manual >> /etc/init/<service>.override is prefered as it leaves the original .conf filke intact. Anyway, its still a shame that such a basic enable/disable took 3 years to develop, and theres no GUI for that. – MestreLion May 27 '11 at 3:09
feedback

For those of us who run Ubuntu over ssh, I think the nicest option is rcconf - a command line ASCII GUI:

sudo apt-get install rcconf
sudo rcconf

alt text

Navigate with tab and arrow keys, press spacebar to enable/disable. Changes are persistent across restarts.

Screenshot borrowed from this blogpost, which also shows sysv-rc-conf - a similar tool that also lets you set the runlevel. (For those who happen to care enough about runlevels to wish to change them :)

Unfortunately, rcconf doesn't work with upstart (services listed in /etc/init/*), just with the traditional mechanism (ls -l /etc/init.d/* - the ones that are not symbolic links).

Fortunately, many of the services that are relevant when ssh-ing in to a server (Apache, Tomcat, mdadm, boinc-client...) haven't been moved to upstart yet.

link|improve this answer
2  
Does this still work with upstart? – oKtosiTe Jan 6 '11 at 20:18
1  
Unfortunately, no. But it has worked for all the cases I have wanted to change - the upstart jobs seems to be mostly things I never want to disable - hardware clock, log daemon, network etc. (on Ubuntu server, at least). But it's something to be aware of (I wasn't :), I've updated the post. – j-g-faustus Jan 6 '11 at 20:59
Unfortunately mysql was converted to upstart. And thats a service i only use when im doing some project. – MestreLion May 27 '11 at 3:12
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.