Starting Services with init


Once your boot loader has finished the initial steps to get your kernel up and running, the init daemon—the parent of all processes—is started. Then init launches the daemons necessary for normal system operation based on entries in its /etc/inittab configuration file.

The /etc/inittab File

The heart of the /etc/inittab configuration file is the following seven lines that define seven system runlevels:

 l0:0:wait:/etc/rc.d/rc 0 l1:1:wait:/etc/rc.d/rc 1 l2:2:wait:/etc/rc.d/rc 2 l3:3:wait:/etc/rc.d/rc 3 l4:4:wait:/etc/rc.d/rc 4 l5:5:wait:/etc/rc.d/rc 5 l6:6:wait:/etc/rc.d/rc 6 

Note 

The system can be at only one runlevel at a time.

These lines indicate that the rc program (the /etc/rc.d/rc script) should run each time the runlevel of the system changes and that init should pass a single character argument consisting of the runlevel number (0 through 6) to the rc program. The rc program then launches all of the scripts that start with the letter S in the corresponding runlevel subdirectory underneath the /etc/rc.d directory.

The default runlevel, the runlevel for normal server operation, is defined in the /etc/inittab file on the initdefault line:

 id:3:initdefault: 

Note 

Use the runlevel command to see the current runlevel.

The default runlevel for a Red Hat Linux server that is not running a graphical user interface is runlevel 3. This means the init daemon runs the rc program and passes it an argument of 3 when the system boots. The rc program then runs each script that starts with the letter S in the /etc/rc.d/rc3.d, passing them the argument start.

Later, when the system administrator issues the shutdown command, the init daemon runs the rc program again, but this time the rc program runs all of the scripts in the /etc/rc.d/rc3.d directory that start with the letter K (for"kill") and passes them the argument stop.

On Red Hat, you can use the following command to list the contents of all of the runlevel subdirectories:

 #ls -l /etc/rc.d/rc?.d | less 

Note 

The question mark in this command says to allow any character to match. So this command will match on each of the subdirectories for each of the runlevels: rc0.d, rc1.drc2.d, and so forth.

The start and kill scripts stored in these subdirectories are, however,not real files. They are just symbolic links that point to the real script files stored in the /etc/rc.d/init.d directory. All of the start and stop init scripts are, therefore, stored in one directory (/etc/rc.d/init.d), and you control which services run at each runlevel by creating or removing a symbolic link within one of the runlevel subdirectories.

Fortunately, software tools are available to help manage these symbolic links. We will explain how to use these tools in a moment, but first, let's examine one way to automatically restart a daemon when it dies by using the respawn option in the /etc/inittab file.

Respawning Services with init

You can cause the operating system to automatically restart a daemon when it dies by placing the name of the executable program file in the /etc/inittab file and adding the respawn option. init will start the daemon as the system enters the runlevel and then watch to make sure the daemon stays running (restarting the daemon, if it dies), as long as the system remains at the same runlevel.

A sample /etc/inittab entry using the respawn option looks like this:

 sn:2345:respawn:/usr/local/scripts/start_snmpd > /dev/null 

This entry tells the init daemon to run the script /usr/local/scripts/start_snmpd at runlevels 2, 3, 4, and 5, and to send any output produced to the /dev/null "device" (the output is discarded).

Notice, however, that we have entered a script file here (/usr/local/scripts/start_snmpd) and not the actual snmpd executable. How will init know which running process to monitor if this script simply runs the snmpd daemon and then ends? init will assume the program has died each time the script /usr/local/scripts/start_snmpd finishes running, and it will dutifully run the script again.

Note 

When this happens, you are likely to see an error message from init saying the sn identifier (referring to the first field in the /etc/inittab file) is respawning too fast and init as decided to give up trying for a few minutes.

So to make sure the respawn option works correctly, we need to make sure the script used to start the snmpd daemon replaces itself with the snmpd daemon. This is easy to do with the bash programming statement exec. So the simple bash script /usr/local/scripts/start_snmpd looks like this:

 #!/bin/bash exec /usr/sbin/snmpd -s -P /var/run/snmpd -l /dev/null 

The first line of this script starts the bash shell, and the second line causes whatever process identification number, or PID, is associated with this bash shell to be replaced with the snmpd daemon (the bash shell disappears when this happens). This makes it possible for init to run the script, find out what PID it is assigned, and then monitor this PID. If init sees the PID go away (meaning the snmpd daemon died), it will run the script again and monitor the new PID thanks to the respawn entry in the /etc/inittab file.[2]

Managing the init Script Symbolic Links with chkconfig

The chkconfig program creates and removes the symbolic links used by init's servant, the /etc/rc.d/rc script. These symbolic links are the real way to enable or prevent a service from automatically running at a particular runlevel. As previously noted, all of the symbolic links for all system runlevels point back to the real scripts stored in the /etc/rc.d/init.d directory (actually, according to the Linux Standards Base,[3] this directory is the /etc/init.d directory, but on Red Hat, these two directories point to the same files[4]).

If you want a service (called myservice) to run at runlevels 2, 3, and 4, this means you want chkconfig to create three symbolic links in each of the following runlevel subdirectories:

     /etc/rc.d/rc2.d     /etc/rc.d/rc3.d     /etc/rc.d/rc4.d 

These symbolic links should start with the letter S to indicate you want to start your script at each of these runlevels. chkconfig should then create symbolic links that start with the letter K (for "kill") in each of the remaining /etc/rc.d/rc<runlevel>.d directories (for runlevels 0, 1, 5, and 6).

To tell chkconfig you want it to do this for your script, you would add the following lines to the script file in the /etc/rc.d/init.d directory:

 # chkconfig: 234 99 90 # description: myscript runs my daemon at runlevel 2, 3, or 4 

You can then simply run the chkconfig program, which interprets this line to mean "create the symbolic links necessary to run this script at runlevel 2, 3, and 4 with the start option, and create the symbolic links needed to run this script with the kill option at all other runlevels."

The numbers 99 and 90 in this example are the start and stop priority of this script. When a symbolic link is created in one of the runlevel subdirectories, it is also given a priority number used by the rc script to determine the order it will use to launch (or kill) the scripts it finds in each of the runlevel subdirectories. A lower priority number after the S in the symbolic link name means the script should run before other scripts in the directory. For example, the script to bring up networking, /etc/rc.d/rc3.d/S10network, runs before the script to start the Heartbeat program /etc/rc.d/rc3.d/S34heartbeat.

The second commented line in this example contains a (required) description for this script. The chkconfig commands to first remove any old symbolic links that represent an old chkconfig entry and then add this script (it was saved in the file /etc/rc.d/init.d/myscript) are:

 chkconfig --del myscript chkconfig --add myscript 

Note 

Always use the --del option before using the --add option when using chkconfig to ensure you have removed any old links.

The chkconfig program will then create the following symbolic links to your file:

 /etc/rc.d/rc0.d/K90myscript /etc/rc.d/rc1.d/K90myscript /etc/rc.d/rc2.d/S99myscript /etc/rc.d/rc3.d/S99myscript /etc/rc.d/rc4.d/S99myscript /etc/rc.d/rc5.d/K90myscript /etc/rc.d/rc6.d/K90myscript 

Again, these symbolic links just point back to the real file located in the /etc/init.d directory (from this point forward, I will use the LSB directory name /etc/init.d instead of the /etc/rc.d/init.d directory originally used on Red Hat systems) throughout this book. If you need to modify the script to start or stop your service, you need only modify it in one place: the /etc/ init.d directory.

You can see all of these symbolic links (after you run the chkconfig command to add them) with the following single command:

 #find /etc -name '*myscript' -print 

You can also see a report of all scripts with the command:

 #chkconfig --list 

Managing the init Script Symbolic Links with ntsysv

The ntsysv command relies on these same commented chkconfig lines in each of the scripts in the /etc/rc.d/init.d directory to manage the symbolic links to start and stop scripts at each runlevel.

Normally, ntsysv only manages the currently running level of the system (not very useful), so you will probably want to tell it to manage all of the runlevels by entering the command:

 #ntsysv --level 0123456 

This will bring up a list of all of the scripts in the /etc/rc.d/init.d directory. By adding an asterisk next to the script name in this listing (and selecting OK), you tell the ntsysv program to create all of the symbolic links just as if you had run the chkconfig --add <scriptname> command. If you remove the asterisk next to the script name, the symbolic links are removed just as if you had entered the chkconfig --del <scriptname> command.

Note 

chkconfig and ntsysv commands do not start or stop daemons (neither command actually runs the scripts). They only create or remove the symbolic links that affect the system the next time it enters or leaves a runlevel (or is rebooted). To start or stop scripts, you can enter the command service <script-name> start or service <script-name> stop, where <script-name> is the name of the file in the /etc/init.d directory.

Removing Services You do Not Need

For each script you do not need (see the next section for a description), you can disable or remove the services by doing the following:

Kill the currently running daemon (you can skip this step and simply reboot the system when you are finished):

 #/etc/init.d/<script-name> stop 

Or on Red Hat systems, the following command works just as well:

 #service <script-name> stop 

Remove symbolic links to the script that cause it to start or stop at the predefined chkconfig runlevels with the command:

 #chkconfig --del <script-name> 

or

 #ntsysv --level 0123456 

Then, inside of the ntsysv utility, remove the * next to each script name to cause it not to run at boot time.

[2]Note that the snmpd daemon also supports a -f argument that accomplishes the same thing as the exec statement in this bash script. See the snmpd man page for more information.

[3]See http://www.linuxbase.org/spec for the latest revision to the LSB.

[4]Because /etc/init.d is a symbolic link to /etc/rc.d/init.d on current Red Hat distributions.



The Linux Enterprise Cluster. Build a Highly Available Cluster with Commodity Hardware and Free Software
Linux Enterprise Cluster: Build a Highly Available Cluster with Commodity Hardware and Free Software
ISBN: 1593270364
EAN: 2147483647
Year: 2003
Pages: 219
Authors: Karl Kopper

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net