474 478




Using Linux:Managing Daemons






-->















Previous
Table of Contents
Next




Editing Startup and Shutdown Scripts
The startup and shutdown scripts reside in the /etc/rc.d directory subtree. These are generally bash scripts that start daemons. Each run level has its own subdirectory of /etc/rc.d, which contains the run level number (as in /etc/rc.d/rc0.d for run level 0, /etc/rc.d/rc3.d for run level 3, and so on). These subdirectories contain symbolic links to actual scripts in /etc/rc.d/init.d. This ensures that all run levels read from the same set of scripts. There is something else to account for, however: Certain processes and daemons must start before other processes. For example, it makes little sense for the system to start the web server (httpd) before having started the networking on the system because the web server needs a network interface on which to initialize.
So this is how it happens: Depending on whether the script is designed to start a service or to ensure that a service is stopped, the first letter of the script name is either S (for start) or K (for kill). Then comes a (by convention) two-digit number, and then the name of the script in the init.d directory. When the system changes to a different run level, the K scripts are run first, in numerical order, and then the S scripts, again in numerical order.
The chkconfig utility checks the recommended numbers for the script you’re using, for both start and kill scripts (the example in the previous section using httpd’s init script lists 85 as the startup number and 15 as the kill number). You should, for the most part, use these numbers unless you have a good reason to do otherwise.
The init scripts do make things easy for you, however—the difficult scripting has been done for you and packaged into functions that your script can call to do most of the dirty work.
A Sample init Script

The init script in Listing 28.1 is for httpd, the Apache web server that comes packaged with Red Hat Linux. I use this as an example because the majority of additional services out there work very similarly to thisw
LISTING 28.1 Listing of/etc/rc.d/init.d/httpd, the init script for the web server


01: #!/bin/sh
02: #
03: # Startup script for the Apache Web Server
04: #
05: # chkconfig: 345 85 15
06: # description: Apache is a World Wide Web server. It is used to
serve \
07: # HTML files and CGI.
08: #
09: #
10:
11:
12: # Source function library.
13: . /etc/rc.d/init.d/functions
14:
15: # See how we were called.
16: case “$1” in
17: start)
18: echo -n “Starting httpd: ”
19: daemon httpd
20: echo
21: touch /var/lock/subsys/httpd
22: ;;
23: stop)
24: echo -n “Shutting down http: ”
25: kill `cat /var/run/httpd.pid`
26: echo httpd
27: rm -f /var/lock/subsys/httpd
28: rm -f /var/run/httpd.pid
29: ;;
30: status)
31: status httpd
32: ;;
33: restart)
34: $0 stop
35: $0 start
36: ;;
37: *)
38: echo “Usage: httpd.init {start|stop|restart|status}”
39:
40: exit 1
41: esac
42:
43: exit 0


The easiest way to set up another program as a daemon in your system is to copy an existing script to a new file, and edit the file to suit your needs. The only lines in the script shown in Listing 28.1 that are specific to httpd are lines 3–7, 18–21, 24–28, 31, and 33. These lines either provide feedback while the script is executing, or actually start the program.
Lines 25 and 28 are specific to Apache because Apache stores its process ID numbers in a file in the /var/run/httpd.pid file to make stopping the process easier. If the daemon you want to add cannot be configured to do this, the script called functions that is included at the beginning contains a function to automatically find the PID(s) of the daemon in question and to kill the process. For example, if your program were called mydaemon, lines 24–28 would look like the following:


24: echo -n “Shutting down mydaemon : “
25: killproc mydaemon
26: echo mydaemon
27: rm -f /var/log/subsys/mydaemon
28: ;;


Note that the line to remove the PID file in /var/run is not there (of course, you could leave it in if mydaemon were to support it).
init script checklist

Using this short checklist should ensure that all necessary features are present to make working with the script you created easy and painless for both you and other systems administrators.


•  The script supports start and stop command-line arguments.
•  The script has appropriate lines for chkconfig to manage the script. If you’re in doubt of the order the program should be started, start it with a high number and stop it with a low number.
•  Make sure that the script handles creating the lock files in /var/lock/subsys and the PID files in /var/run; the status command-line argument and the killproc function for the script use these. A status check is nice to have for a daemon process.
•  Make sure that the script provides appropriate feedback to make clear to the user what is happening. Be especially sure of this if the daemon that’s starting needs to start connections that need some time to initialize. Otherwise, observers might get the impression that the system has hung, and take some drastic actions that might not be warranted.





Previous
Table of Contents
Next














Wyszukiwarka

Podobne podstrony:
474 478
474 (2)
478 18
478 19
474 477
Kochać inaczej De Mono linia 4 45 d 48 478 0 nev
readme (474)
478 15
index (478)
478 07
01 (474)
478 22

więcej podobnych podstron