Daemons and Services
Previous  Top  Next

Windows NT uses services to manage applications that must be active all the time, even when a user is not logged in. Services can also be controlled by starting or stopping them (pause and resume are also supported rarely). Now Linux doesn't have "services", but instead has daemons, which is essentially the same. Daemons (or services) like apache or samba fall into this category. They are specially crafted applications that are able to run in the background, staying active at all times. The bad news about Linux's daemons is that they are more complicated to configure, the good news is that you can configure almost everything you want – and sometimes you even have to.

Daemons are started with shell scripts which almost always come with the respective daemons (samba, apache , …). But don't worry, you will not have to understand shell scripts to configure daemons – it will be easier than that.

The scripts that start the daemons are located in the directory /etc/rc.d/init.d. So if you want to know if a particular service can be controlled in that manner, this is the place to look for it. It is actually surprisingly easy to write your own startup script, assuming the daemon's requested environment isn't too complicated … Now there is one thing that all of those scripts must have in common: They must support at least the command line arguments "start" and "stop". Sometimes they also support "restart" or "status", but "start" and "stop" are essential. Why? This is the way we (the penguin's servants) and Linux control the daemons.

But how does Linux know which services to start/stop at what time? The run levels and the /etc/rc.d directories are the answer. After the master process init has been spawned, Linux is always in a run level. If you don't use X then this would be run level 3, if you use X then you're running in runlevel 5. You don't tell Linux just to start a service, you tell it at what run level to start a service, and at what run level to stop a service. And on top of that you can tell it in what sequence to start (like the service dependencies on Windows) – e.g. you could start apache before samba and vice versa – just as an example. Now every used run level (I say used because some run levels are not really being used, such as run level 4) has its /etc/rc.d directory. For example run level three has the directory /etc/rc.d/rc3.d, run level 5 has /etc/rc.d/rc5.d.

Let's assume that we have a script that starts up our beloved apache web server in the /etc/rc.d/init.d directory, called httpd. Just the simple presence of that script in the init.d directory won't make apache do anything upon boot. This would be like a service in Windows that is set to manual. You can start it manually, but upon boot it won't do anything. Let's consider this example: We want apache to start at run level 3 (and not at run level 5) – and shut down at run level 0 (shutdown) and run level 6 (reboot).

To accomplish this goal we have to use symbolic links. A symbolic link is like a shortcut under Windows – it's a file that points to another file. When you edit the symbolic link, you really edit the file it points to, but when you delete the symbolic link, you really only delete the symbolic link – fortunately. So now we create a symbolic link in the directory /etc/rc.d/rc3.d with the name S80httpd. Excuse me? S80httpd? Why not F234apache? The first letter of the symbolic link tells Linux how to control the service. An uppercase S (it must be UPERCASE) means to start the service (yes, and all that Linux really does is call the script with the previously explained "start" option, this is why it is essential to support "start" and "stop"), and an uppercase K means to stop (or kill) the service. Again, Linux simply calls the script with the "stop" argument. The number after S or K, 80 in this example, determines the start order. Linux begins executing the scripts with 0 until there is no more. You can use an existing number if you want to, I assume the alphabetical order will then determine which one to start first. The name after the number is really up to you, but to be halfway consistent you should really use the name of the script in the init.d directory. So here are the commands I would type to make this miracle happen:


cd /etc/rc.d/rc3.d

ln –s /etc/rc.d/init.d/httpd s80httpd

cd /etc/rc.d/rc0.d

ln –s /etc/rc.d/init.d/httpd k20httpd

cd /etc/rc.d/rc6.d

ln –s /etc/rc.d/init.d/httpd k20httpd

Of course I did more than would I had described above – the first two lines are enough to configure the daemon to start. The next four lines add the necessary symbolic links to shutdown the daemon properly when Linux is shutdown or restarted. I used 20 as the sequence number since httpd should be stopped in the beginning rather than in the end. Think about it, httpd depends on other network services that shouldn't be stopped before httpd! To round this chapter up I included a picture to make you visualize things a little easier:

rc

Here you see the 7 directories for the different run levels (rc0.d … rc6.d) and the symbolic links contained in them, K30sendmail and S80sendmail showed as examples. The init.d directory of course contains the original control files, such as nfs, mysql or sendmail serving as some examples here.

Please note the files rc.local and rc.sysinit here. They both serve an important purpose as well and you should be familiar with them. The rc.sysinit file is an integral part of Redhat since it sets up several components of Linux, networking for example. I recommend looking through the file in an editor, you will see a lot of interesting things.
The rc.local file is for Linux what autoexec.bat is (was J) for Ms-Dos. This shell script is executed after all the init scripts and you can place your own initialization in there. You might want to change your login banner in there, just look for /etc/issue.