• No results found

The default runlevel on Ubuntu Server is runlevel 2, in which all the services that have to be started are referred to. Before entering runlevel 2, Ubuntu Server passes through runlevel S. In this runlevel, all the essential services that are always required on your server are started. The configuration of both works in more or less the same way.

To understand the working of a runlevel, you need to understand two components: the service scripts and the scripts that execute these service scripts. All of the service scripts are found in the /etc/init.ddirectory, and they are used to start fundamental services such as the mounting of file systems as well as network services like your Apache server. To specify which of these scripts have to be executed when starting your server, two runlevel-related directories are used. The first of these directories is /etc/rcS.d, and, on a system that follows a default installation, the second of them is /etc/rc2.d. In the /etc/rcS.ddirectory, services are started that are always needed, whereas, in the /etc/rc2.ddirectory, services are started that are specific to a given runlevel.

To make sure that a service starts automatically during system initialization, a symbolic link is created in the /etc/rcS.ddirectory. The name of this link starts with an S, followed by a two-digit number, followed by the name of the script in /etc/init.dthat the link refers to. All these links are processed when booting your server, and they are processed in alphabetical order. So S01blahis processed before S99blah.

The same thing happens for the runlevel directories, with the difference that, when work- ing with runlevels, there is an option to change the current runlevel. When changing a runlevel, some scripts may have to be started as well. To do this, more symbolic links are created. The name of these links starts with K, followed by a two-digit number. Listing 6-5 shows an example of the default runlevel 2.

Listing 6-5.To Determine What Is Started and What Is Stopped in a Runlevel, Some Symbolic Links Are Processed.

root@ubuntu:/etc/rc2.d# ls -l total 4

-rw-r--r-- 1 root root 556 2007-04-10 17:46 README

lrwxrwxrwx 1 root root 18 2007-07-29 07:34 S10sysklogd -> ../init.d/sysklogd lrwxrwxrwx 1 root root 15 2007-07-29 07:34 S11klogd -> ../init.d/klogd lrwxrwxrwx 1 root root 15 2007-07-29 07:36 S15bind9 -> ../init.d/bind9

lrwxrwxrwx 1 root root 23 2007-07-29 07:36 S17mysql-ndb-mgm -> ../init.d/mysql-ndb-mgm lrwxrwxrwx 1 root root 19 2007-07-29 07:36 S18mysql-ndb -> ../init.d/mysql-ndb lrwxrwxrwx 1 root root 15 2007-07-29 07:36 S19mysql -> ../init.d/mysql

lrwxrwxrwx 1 root root 17 2007-07-29 07:32 S20makedev -> ../init.d/makedev lrwxrwxrwx 1 root root 15 2007-07-29 07:36 S20rsync -> ../init.d/rsync lrwxrwxrwx 1 root root 13 2007-07-29 11:44 S20ssh -> ../init.d/ssh lrwxrwxrwx 1 root root 13 2007-07-29 07:36 S89atd -> ../init.d/atd lrwxrwxrwx 1 root root 14 2007-07-29 07:36 S89cron -> ../init.d/cron lrwxrwxrwx 1 root root 17 2007-07-29 07:36 S91apache2 -> ../init.d/apache2 lrwxrwxrwx 1 root root 18 2007-07-29 07:33 S99rc.local -> ../init.d/rc.local lrwxrwxrwx 1 root root 19 2007-07-29 07:33 S99rmnologin -> ../init.d/rmnologin

If you want to make sure that a given service is started automatically, it follows that you first need to make sure that it has a service script in /etc/init.d. If it does, you next need to make a symbolic link for this service. If it is a service that has to be started when your server is booting, you just need a start link in /etc/rcS.d. If it is a service that you want to be included in your server’s runlevels, you need to create a start link as well as a stop link in the directory of the default runlevel, which would be /etc/rc2.din most cases. So let’s see how this works for the imaginary service blahd.

1. To include blahdin system startup, make sure that it has a start script in /etc/init.d. If blahdwas developed to be used on either Debian or Ubuntu Linux, it will have such a script. Let’s say that the name of this script is /etc/init.d/blah.

2. If blahdis a nonessential service, you should include it in the default runlevel. There- fore, you’re going to create two symbolic links in /etc/rc2.d, and, to put the service in the right place, you should first analyze its dependencies. If it depends on some other service to be started first, give it a relatively high number after the S, such as S50. If it doesn’t depend on anything, you can give it a relatively low number. The inverse is true for the kill scripts that make sure that the service is stopped once you quit the runlevel: scripts that depend on many other services but don’t have depend- encies themselves get a low number; scripts that don’t depend on other services get a high number.

3. Now create the links. To create the start link, first use cd /etc/rc2.dand then ln -s ../init.d/blah S10blahd. Next, to create the kill link, use ln -s ../init.d/blah K90blahd. When restarting your server, the service will now be executed automatically.

Tip

When determining the proper load number for a script, on Ubuntu Server you can always assume that all device drivers are initialized, local file systems have been mounted, and networking is available after the S40 scripts have been processed. So, in case of doubt, use S41 or higher.

In document Beginning Ubuntu Server Administration (Page 184-186)