Previous Section
 < Day Day Up > 
Next Section


Service Scripts: /etc/init.d

Most server software using RPM Red Hat packages will automatically install the startup scripts and create the needed links in the appropriate rcN.d directories, where N is the runlevel number. Startup scripts, though, can be used for any program you may want run when your system starts up. To have such a program start automatically, you first create a startup script for it in the /etc/rc.d/init.d directory, and then create symbolic links to that script in the /etc/rc.d/rc3.d and /etc/rc.d/rc5.d directories. A shutdown link (K) should also be placed in the rc6.d directory used for runlevel 6 (reboot).

Service Script Functions

A simplified version of the startup script httpd uses on Red Hat systems is shown in a later section. You can see the different options, listed in the /etc/rc.d/init.d/httpd example, under the case statement: start, stop, status, restart, and reload. If no option is provided (*),the script use syntax is displayed. The httpd script first executes a script to define functions used in these startup scripts. The daemon function with httpd actually executes the /usr/sbin/httpd server program.

echo -n "Starting httpd: "
 daemon httpd
 echo
 touch /var/lock/subsys/httpd

The killproc function shuts down the daemon. The lock file and the process ID file (httpd.pid) are then deleted:

 killproc httpd
 echo
 rm -f /var/lock/subsys/httpd
 rm -f /var/run/httpd.pid

The daemon, killproc, and status functions are shell scripts defined in the functions script, also located in the inet.d directory. The functions script is executed at the beginning of each startup script to activate these functions. A list of these functions is provided in Table 20-4.

. /etc/rc.d/init.d/functions
Table 20-4: Init Script Functions

Init Script Function

Description

daemon [+/-nicelevel] program [arguments] [&]

Starts a daemon, if it is not already running.

killproc program [signal]

Sends a signal to the program; by default it sends a SIGTERM, and if the process doesn't stop, it sends a SIGKILL. It will also remove any PID files, if it can.

pidofproc program

Used by another function, it determines the PID of a program.

status program

Displays status information.

Service Script Tags

The beginning of the startup script holds tags used to configure the server. These tags, which begin with an initial # symbol, are used to provide runtime information about the service to your system. The tags are listed in Table 20-5, along with the startup functions. You enter a tag with a preceding # symbol, the tag name with a colon, and then the tag arguments. For example, the processname tag specifies the name of the program being executed, in this example httpd:

Table 20-5: System V init Script Tags

Init Script Tags

Description

# chkconfig: startlevellist startpriority endpriority

Required. Specifies the default start levels for this service as well as start and end priorities.

# description [ln]: description of service

Required. The description of the service, continued with \ characters. Use an initial # for any added lines. With the ln option, you can specify the language the description is written in.

# autoreload: true

Optional. If this line exists, the daemon checks its configuration files and reloads them automatically when they change.

# processname: program

Optional, multiple entries allowed. The name of the program or daemon started in the script.

# config: configuration-file

Optional, multiple entries allowed. Specifies a configuration file used by the server.

# pidfile: pid-file

Optional, multiple entries allowed. Specifies the PID file.

# probe: true

Optional, used in place of autoreload, processname, config, and pidfile entries to automatically probe and start the service.

# processname: httpd

If your script starts more than one daemon, you should have a processname entry for each. For example, the Samba service starts up both the smdb and nmdb daemons.

# processname: nmdb

The end of the tag section is indicated by an empty line. After this line, any lines beginning with a # are treated as comments. The chkconfig line lists the default runlevels that the service should start up on, along with the start and stop priorities. The following entry lists runlevels 3, 4, and 5 with a start priority of 85 and a stop of 15:

# chkconfig: 345 85 15

For the description, you enter a short explanation of the service, using the \ symbol before a newline to use more than one line.

# description: Apache Web server

With config tags, you specify the configuration files the server may use. In the case of the Apache Web server, there may be three configuration files:

# config: /etc/httpd/conf/access.conf
# config: /etc/httpd/conf/httpd.conf
# config: /etc/httpd/conf/srm.conf

The pidfile entry indicates the file where the server's process ID is held.

Service Script Example

As an example, a simplified version of the Web server startup script, /etc.rc.d/init.d/httpd, is shown here. Most scripts are much more complicated, particularly when determining any arguments or variables a server may need to specify when it starts up. This script has the same name as the Web server daemon, httpd:

#!/bin/sh
#
# Startup script for the Apache Web Server
#
# chkconfig: 35 85 15
# description: Apache is a World Wide Web server. \
# It is used to serve HTML files and CGI.
# processname: httpd
# pidfile: /var/run/httpd.pid
# config: /etc/httpd/conf/access.conf
# config: /etc/httpd/conf/httpd.conf
# config: /etc/httpd/conf/srm.conf
# Source function library.
. /etc/rc.d/init.d/functions
   
# See how we were called.
case "$1" in
 start)
  echo -n "Starting httpd: "
   daemon httpd
   echo
   touch /var/lock/subsys/httpd
   ;;
  stop)
   killproc httpd
   echo
   rm -f /var/lock/subsys/httpd
   rm -f /var/run/httpd.pid
   ;;
 status)
   status httpd
   ;;
 restart)
   $0 stop
   $0 start
   ;;
 reload)
   echo -n "Reloading httpd: "
   killproc httpd -HUP
   echo
   ;;
 *)
   echo "Usage: $0 {start|stop|restart}"
   exit 1
 esac
exit 0

Installing Service Scripts

The RPM packaged versions for an Internet server include the startup script for that server. Installing the RPM package installs the script in the /etc/rc.d/init.d directory and creates its appropriate links in the runlevel directories, such as /etc/rc.h/rc3.d. If you decide, instead, to create the server using its source code files, you can then manually install the startup script. If no startup script exists, you first make a copy of the httpd script— renaming it—and then edit the copy to replace all references to httpd with the name of the server daemon program. Then place the copy of the script in the /etc/rc.d/init.d directory and make a symbolic link to it in the /etc/rc.d/rc3.d directory. Or you could use redhat-config-services to create the link in the /etc/rc.d/rc3.d directory. Select File | Refresh Services. When you start your system now, the new server is automatically started up, running concurrently and waiting for requests.



Previous Section
 < Day Day Up > 
Next Section