SysVinit explained: starting and stopping of services

Welcome to the wonderful world of SystemV and one of the aspects we'll cover is the configuration of startup and shutdown processes. And yes, you do have drag-and-drop options if you don't want to use the console methods.

First an explanation of the whole process

The initialization process (init) in Linux sets the system to a certain runlevel, which can be configured in the /etc/inittab file. Most (Red Hat based) Linux distributions use the following runlevels:

  • 0 - halt
  • 1 - Single user mode
  • 2 - Multiuser mode, without NFS (The same as 3, if you do not have networking)
  • 3 - Full multiuser mode
  • 4 - unused
  • 5 - Starts up with the window manager's login screen if set.
  • 6 - reboot

To set the default runlevel you can edit the following line in your /etc/inittab file:

id:X:initdefault:

where X is the runlevel. So to automatically startup X11, you would replace X with 5. Or if you're at the lilo prompt, you can enter linux X (where X again is the runlevel).

During the init process, the /etc/rc.sysinit file is run which in turn goes into the default runlevel from the /etc/inittab file.

Going a little deeper
Every runlevel runs the available scripts in the appropriate runlevel directory found in /etc/rcX.d where X is the runlevel. If you're entering runlevel 3 the scripts in /etc/rc3.d are executed. These files are symbolic links to the main scripts located in the /etc/init.d directory. A few samples of some of my runlevel 3 scripts:

K20nfs -> ../init.d/nfs
S10network -> ../init.d/network
S55sshd -> ../init.d/sshd

Links beginning with "K" are to be stopped whenever leaving the runlevel they are in (for example when leaving runlevel 3 - full user mode, to runlevel 6 - reboot).

Links beginning with "S" are to be started, whenever the system is entering the runlevel they are about to go in to (for example when entering runlevel 3).
Confused yet? Wait, there's more! The numbers after the "K" or "S" assignments are also a significant part of the process. For example, in my scripts listed above, I have to give sshd a higher numerical value in order for it function properly. Lower numbers are started first so my network has to be initialized first by giving it a lower number. The same goes for other services that require the network to be up first, such as sendmail (This might explain why your system hangs for a short period if you don't have your network configured properly). If you understand the above, configuring your own services should be a snap.

Configuring a daemon to startup

Most applications that require a daemon at startup provide the necessary scripts which you can place in the /etc/init.d directory. Some even configure it for you. A good example is using MySQL to configure the daemon at startup (runlevel 3). I won't go into the MySQL installation itself. If you're installing the rpm version of MySQL the daemons will be configured for you. The source version however, does not install it, but it provides a customized version of the required startup file - mysql-your_version/support-files/mysql.server. Copy this file to /etc/init.d and make it executable.
To make the symbolic links issue the following commands:

ln -s /etc/init.d/mysql.server /etc/rc.d/rc3.d/S99mysql
ln -s /etc/init.d/mysql.server /etc/rc.d/rc0.d/S01mysql

These commands create the runlevel 3 link for starting up and the runlevel 0 link for when halting the system.

Isn't there an easier way?

Yes there are several ways. Two of them are to use chkconfig or ksysv.
Use chkconfig where service is for example nfs:

chkconfig --add service
chkconfig --del service

The only requirement for this is that you have the original script in the /etc/init.d directory.

ksysv is KDE's graphical version of the SystemV init manager. This allows for drag-and-drop manupilation of all the runlevels. There are multiple options such as explanations of available programs, setting the permissions, UID bits and more. If you don't want to mess with the console, this is definitely recommended!

I hope this short howto was somewhat helpful to you. All comments, suggestions and corrections are always welcome.