MandrakeUser.Org - Your Mandrake-Linux Knowledge Base!


 
 

* DocIndex - Administration

System Services I

* Introducing System Services
* Advantages Of The Service Mechanism
* How Services Work

Related Resources:

SysAdminGuide, 9
MdkReference II, 11
man init

Revision / Modified: Jan. 03, 2001
Author: Tom Berger

 

* Introducing System Services

If you happen to open 'System' - 'Services' in the Mandrake Control Center, you see a lot of columns, starting with a more or less cryptic name for the service. The second column reads either 'stopped' or 'running', the third is a button which reveals some basic information. Then there's a check box labeled 'On boot' followed by two buttons: 'Start' and 'Stop'.

This layout describes in essence what you can do with a service: you can start or stop it and you can configure it to be 'started' automatically at boot time.

But what is a service? In contrast to a program, servicesdo not require user input (they 'run in the background') apart from starting or stopping them, and even this can be automated.
There are two kinds of services:

  • Services which are started and keep running for the duration of a session (i.e. until the system gets shut down). In Unix slang these are also called daemons ('helpful spirit'). These are usually servers of some kind which are started and then wait for incoming requests, like a web server, a mail server, the printer service or a font server.

  • Services which are started, run and terminated when finished. These are usually scripts for system maintenance or for enabling certain features, like the 'numlock' script whose sole purpose is to turn on the numlock feature - i.e. being able to use the right-hand number pad on most keyboards for number input - during boot.

Apart from the Mandrake Control Center, there's a slew of other graphical configuration utilities you can use to configure services. Webmin and Linuxconf both come with modules to do that. KDE and GNOME each offer there own brand of service configuration utility. Furthermore there's 'tksysv' and its console based parent 'ntsysv'.
But the MCC module does what needs to be done, but maybe you like one of the other applications better. You don't need to be afraid of causing inconsistencies when using different utilities since they all use the same (command line) commands, 'service' and 'chkconfig'.

'service' and 'chkconfig'

The 'service' command, a simple shell script in '/sbin', is used to display the status of a service, to start, stop or restart it. This command takes two arguments, the name of the service (i.e. the name of the file in '/etc/init.d') and what should be done in regard to this service:

  • # service service_name start
  • # service service_name stop
  • # service service_name restart
  • # service service_name status

'restart' and 'status' are not supported by all service scripts.

'chkconfig' lists, adds, removes and configures services permanently. To have a service started automatically at boot time, you would use:

# chkconfig service_name on

To have it not started automatically:

# chkconfig service_name off

To list all available services and their current configuration:

# chkconfig --list

The output of this last command will become clearer to you when you've read the next section. More on this command can be found in man chkconfig.

Like their graphical counterparts, these commands require you to be 'root'. Nothing forces you to use them instead of MCC or the other utilities, I prefer them because I'm faster at typing a command than at clicking through a graphical interface ;-).

* section index * top

* Advantages Of The Services Mechanism

Being able to control services has several advantages:

  • Reducing system load:
    Although daemons are 'sleeping' most of time they nevertheless use up a certain amount of system memory. The 'service' interface allows you to start services on demand, for example you can start the printer daemon right before printing and stop it when finished.

  • Increasing system security:
    Daemons are listening on certain ports for events. More daemons running mean more open ports which in turn provide more possible points of attack. On the other hand there are services which actively increase system security, like the 'bastille-firewall' service.

  • Avoiding reboots:
    If you change the configuration of a daemon, the daemon usually has to be started to let the change take effect. If you install a package which contains a service, the service usually won't start right away but will be configured to be started automatically at boot time.
    By controlling services you can fulfill these tasks during runtime.

  • Shortening boot time:
    A good chunk of the time your Linux system needs to boot is taken up by starting or running daemons and other services. If you configure your system to start only those services on boot you need immediately or all the time, you can reduce the boot up time considerably.

* section index * top

* How Services Work

This section is intended for people who not only want to know what to do but also why things are done this way. You can live on Linux without this, but in my opinion it's more fun when you get a grasp of the concept behind the scenes.

Service Scripts

If you are curious, you might want to know now how the system knows which services are available. The service scripts are located in '/etc/init.d' ('/etc/rc.d/init.d' on older releases).
Graphical utilities like the Mandrake Control Center just assume that every script in this directory controls a service, so if you put a script there, it will appear on the Services module of the Mandrake Control Center and in similar utilities, too, and can also be handled directly via the commands 'service' and 'chkconfig'.

A service script contains the commands to at least start or stop a service. Have a look at a basic template for a service script:


#!/bin/sh
# chkconfig: runlevels order_start_link order_stop_link
# description: short description of service
. /etc/rc.d/init.d/functions
case "$1" in
start)
echo -n "Starting service: "
command(s) to start service echo ;; stop) echo -n "Shutting down service: "
command(s) to stop service echo ;; status) status service_name ;; *) echo "*** Usage: service_name {start|stop|status}"
exit 1
esac
exit 0

If you've already seen a shell script, it's pretty simple. 'chkconfig' and 'description' are explained in the next subsection. The 'functions' line is only needed here to have the 'status' command available. Then there's a 'case' fork which tells the script which commands to execute if the last argument to the 'service' command is either 'stop', 'start' or 'status'. The 'echo' lines provide some feedback, '*)' matches all cases in which the last argument isn't one of 'start', 'stop' or 'status' and thus invalid (prints a usage message and exits).
Of course you have to make sure that service_name really is the name of the script and that the script has the executable bit set.

Runlevel Links

Some services depend on other services. The 'httpd' service (Apache web server) for example won't start correctly if the 'network' script hasn't already set up the network interfaces. How is the order in which services are started on boot determined?

Have a look at the '/etc/rc.d' directory:

$ ls /etc/rc.d
init.d/ rc0.d/ rc2.d/ rc4.d/ rc6.d/ rc.local* rc.sysinit* rc* rc1.d/ rc3.d/ rc5.d/ rc.firewall rc.modules*

You see the 'init.d' from '/etc' here again (in fact it's the same) and then several directories and files starting with 'rc' ('rc' is short for 'runcom[mand]').
In Mandrake Linux releases 8.0 and later, these files and directories are also accessible directly from the '/etc' directory.

If you now look into one of those 'rcnumber' subdirectories, you will find a bunch of files, some of them starting with 'S' and some of them with 'K' followed by a two-digit number. 'S' is short for 'start' and 'K' stands for 'kill'. The numbers imply the order in which starting and killing services takes place. In fact all those files are just links to their appropriate counterparts in '/etc/init.d'.
'S12syslog' for example is a link to '/etc/init.d/syslog' and gets started after 'S10network' which links to '/etc/init.d/internet' but before 'S20random'.

You don't have to create these links yourself when configuring a standard service with 'chkconfig' because most scripts already contain a 'chkconfig' line, like for example the 'network' service script:

#!/bin/bash
#
# network Bring up/down networking
#
# chkconfig: 2345 10 90
# description: Activates/Deactivates all network interfaces
# configured to start at boot time.

The standard configuration for this script is to have it started in the runlevels 2, 3, 4 and 5 with a 'S10network' link in the directories '/etc/rc.d/rc.2' to '/etc/rc.d/rc.5' and stopped in runlevels 0, 1 and 6 with a 'K90network' in the directories '/etc/rc.d/rc.[0,1,6]'. This standard configuration is applied when using the 'reset' option:

# chkconfig network reset

will create exactly these links, whereas

# chkconfig service_name on

always defaults to starts on runlevels 3, 4 and 5 only.

What are runlevels then? Runlevels are listed in '/etc/inittab':

# Default runlevel. The runlevels used by RHS are:
# 0 - halt (Do NOT set initdefault to this)
# 1 - Single user mode
# 2 - Multiuser, without NFS
# 3 - Full multiuser mode
# 4 - unused
# 5 - X11
# 6 - reboot (Do NOT set initdefault to this)

During operation, the system always is in one of these runlevels, most of the time either in runlevel 3 (console) or runlevel 5 (X, i.e. the graphical interface).
Upon switching runlevels, e.g. by starting the graphical interface or stopping it, by booting the machine or rebooting it etc, the script '/etc/rc.d/rc' is executed. This script in turn looks up the start and kill links in the appropriate 'rcnumber' directory (where number matches the number of the runlevel the system is switching to) and executes them, i.e. starts or stops the services as configured for the runlevel the system is switching to. This explains why '/etc/rc.d/rc.0' and '/etc/rc.d/rc6' almost only contain 'kill' links since all services are stopped when halting or rebooting the machine.

This elaborate system is called the System V Init Process, because it has been introduced with version five of UNIX®. Apart from Slackware, all major Linux distributions use it. Slackware and *BSD operating systems use the BSD-style Init Process which more or less packs the whole initialization and service maintenance work into one file.

How To Put This System To Use

The 'chkconfig' program allows you a finely grained control on what services are started or stopped on which runlevels. Under certain circumstances it can be useful to reconfigure services.
Take the GPM service, for example. GPM is the 'General Purpose Mouse Daemon'. You will need to have it running when you want to use a mouse on runlevel 3 (console). On runlevel 5 (graphical interface), it is next to useless, it can even cause incompatibilities to occur. Using 'chkconfig' you can configure the gpm service only to be run on runlevel 3:

# chkconfig --level 3 gpm on
# chkconfig --level 5 gpm off

This will create a start link in 'rc3' and a kill link in 'rc5'.

The next pages of this article will provide you with an overview of all service scripts available in Mandrake Linux 8.1.

* section index * top

* Annotated List of System Services a-h


 
Legal: All texts on this site are covered by the GNU Free Documentation License. Standard disclaimers of warranty apply. Copyright LSTB (Tom Berger) and Mandrakesoft 1999-2002.