Linux services

From Ever changing code
Jump to navigation Jump to search

This is a begging of the working with Linux services also read daemons 'd' programs.

Ubuntu - Debian based

Overview init.d and upstart

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.

Check if it's an upstart service

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

Disable upstart service

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/mysql.override
OBSOLETE since Ubuntu 11.04

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

CentOS - RPM based

sudo chkconfig --list all

sudo chkconfig collectd off #disable
[ec2-user@server1]$ chkconfig --list | grep collectd
collectd        0:off   1:off   2:off   3:off   4:off   5:off   6:off

sudo chkconfig collectd on #enable

sudo chkconfig [servicename] reset #reset to a service defaults

References