📄 474-478.html
字号:
<HTML>
<HEAD>
<TITLE>Using Linux:Managing Daemons</TITLE>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!--ISBN=0789716232//-->
<!--TITLE=Using Linux//-->
<!--AUTHOR=William Ball//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Que//-->
<!--CHAPTER=28//-->
<!--PAGES=474-478//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="472-474.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="478-482.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H3><A NAME="Heading9"></A><FONT COLOR="#000077">Editing Startup and Shutdown Scripts</FONT></H3>
<P>The startup and shutdown scripts reside in the <TT>/etc/rc.d</TT> directory subtree. These are generally <TT>bash</TT> scripts that start daemons. Each run level has its own subdirectory of <TT>/etc/rc.d</TT>, which contains the run level number (as in <TT>/etc/rc.d/rc0.d</TT> for run level 0, <TT>/etc/rc.d/rc3.d</TT> for run level 3, and so on). These subdirectories contain <I>symbolic links</I> to actual scripts in <TT>/etc/rc.d/init.d</TT>. 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 (<TT>httpd</TT>) before having started the networking on the system because the web server needs a network interface on which to initialize.</P>
<P>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 <TT>S</TT> (for start) or <TT>K</TT> (for kill). Then comes a (by convention) two-digit number, and then the name of the script in the <TT>init.d</TT> directory. When the system changes to a different run level, the <TT>K</TT> scripts are run first, in numerical order, and then the <TT>S</TT> scripts, again in numerical order.</P>
<P>The <TT>chkconfig</TT> utility checks the recommended numbers for the script you’re using, for both start and kill scripts (the example in the previous section using <TT>httpd</TT>’s <TT>init</TT> script lists <TT>85</TT> as the startup number and <TT>15</TT> as the kill number). You should, for the most part, use these numbers unless you have a good reason to do otherwise.</P>
<P>The <TT>init</TT> 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.</P>
<H4 ALIGN="LEFT"><A NAME="Heading10"></A><FONT COLOR="#000077">A Sample <I>init</I> Script
</FONT></H4>
<P>The <TT>init</TT> script in Listing 28.1 is for <TT>httpd</TT>, 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</P>
<P><B>LISTING 28.1</B> Listing of<I>/etc/rc.d/init.d/httpd</I>, the <I>init</I> script for the web server</P>
<!-- CODE //-->
<PRE>
<B>01:</B> #!/bin/sh
<B>02:</B> #
<B>03:</B> # Startup script for the Apache Web Server
<B>04:</B> #
<B>05:</B> # chkconfig: 345 85 15
<B>06:</B> # description: Apache is a World Wide Web server. It is used to
serve \
<B>07:</B> # HTML files and CGI.
<B>08:</B> #
<B>09:</B> #
<B>10:</B>
<B>11:</B>
<B>12:</B> # Source function library.
<B>13:</B> . /etc/rc.d/init.d/functions
<B>14:</B>
<B>15:</B> # See how we were called.
<B>16:</B> case “$1” in
<B>17:</B> start)
<B>18:</B> echo -n “Starting httpd: ”
<B>19:</B> daemon httpd
<B>20:</B> echo
<B>21:</B> touch /var/lock/subsys/httpd
<B>22:</B> ;;
<B>23:</B> stop)
<B>24:</B> echo -n “Shutting down http: ”
<B>25:</B> kill `cat /var/run/httpd.pid`
<B>26:</B> echo httpd
<B>27:</B> rm -f /var/lock/subsys/httpd
<B>28:</B> rm -f /var/run/httpd.pid
<B>29:</B> ;;
<B>30:</B> status)
<B>31:</B> status httpd
<B>32:</B> ;;
<B>33:</B> restart)
<B>34:</B> $0 stop
<B>35:</B> $0 start
<B>36:</B> ;;
<B>37:</B> *)
<B>38:</B> echo “Usage: httpd.init {start|stop|restart|status}”
<B>39:</B>
<B>40:</B> exit 1
<B>41:</B> esac
<B>42:</B>
<B>43:</B> exit 0
</PRE>
<!-- END CODE //-->
<P>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 <TT>httpd</TT> 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.</P>
<P>Lines 25 and 28 are specific to Apache because Apache stores its process ID numbers in a file in the <TT>/var/run/httpd.pid</TT> file to make stopping the process easier. If the daemon you want to add cannot be configured to do this, the script called <TT>functions</TT> 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 <TT>mydaemon</TT>, lines 24–28 would look like the following:</P>
<!-- CODE SNIP //-->
<PRE>
24: echo -n “Shutting down mydaemon : “
25: killproc mydaemon
26: echo mydaemon
27: rm -f /var/log/subsys/mydaemon
28: ;;
</PRE>
<!-- END CODE SNIP //-->
<P>Note that the line to remove the PID file in <TT>/var/run</TT> is not there (of course, you could leave it in if <TT>mydaemon</TT> were to support it).</P>
<H4 ALIGN="LEFT"><A NAME="Heading11"></A><FONT COLOR="#000077"><I>init</I> script checklist
</FONT></H4>
<P>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.
</P>
<DL>
<DD><B>•</B> The script supports <TT>start</TT> and <TT>stop</TT> command-line arguments.
<DD><B>•</B> The script has appropriate lines for <TT>chkconfig</TT> 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.
<DD><B>•</B> Make sure that the script handles creating the lock files in <TT>/var/lock/subsys</TT> and the PID files in <TT>/var/run</TT>; the <TT>status</TT> command-line argument and the <TT>killproc</TT> function for the script use these. A status check is nice to have for a daemon process.
<DD><B>•</B> 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.
</DL>
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="472-474.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="478-482.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -