📄 472-474.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=472-474//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="470-472.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="474-478.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H3><A NAME="Heading4"></A><FONT COLOR="#000077">Using <I>chkconfig</I>
</FONT></H3>
<P><TT>chkconfig</TT> is an automated <TT>init</TT> script-management tool. It acts as a sanity checker to make sure that the system will stop a service when leaving a run level.</P>
<P><TT>chkconfig</TT> requires some additional comment lines in the actual <TT>init</TT> script to tell it in which run levels the service should be started, and when, relatively, the service should be started during the initialization of the run level. (<TT>init</TT> scripts are processed in a specific order to ensure that services dependent on others are started after the services they depend on.) These lines, taken from the <TT>httpd</TT> <TT>init</TT> script, are as follows:</P>
<!-- CODE SNIP //-->
<PRE>
# chkconfig: 345 85 15
# description: Apache is a World Wide Web server. It is
used to serve
# HTML files and CGI.
</PRE>
<!-- END CODE SNIP //-->
<P>The first line, which is a marker for <TT>chkconfig</TT>, tells it that the given script should be run for run levels 3, 4, and 5, with a start order of 85, and a stop order of 15. This means that <TT>http</TT> is one of the last services to start when entering one of these run levels, and one of the first ones to get stopped when leaving one of them.</P>
<TABLE BORDER="2" BORDERCOLOR="#0000" ALIGN="CENTER">
<TR><TD><FONT SIZE="+1"><B>What’s a sanity checker?</B></FONT>
<BR>Tools that have solved—or that at least help you solve—particularly irritating problems for programmers and systems administrators have become known as <I>sanity checkers</I>. In particular, setting up <TT>init</TT> scripts has caused systems administrators significant amounts of grief since the early days of the UNIX operating system. Many systems administrators have whiled away days (and nights!) looking for a problem with their system configuration or trying to configure things. Frustration over extended periods of time, combined with lack of sleep, too much coffee, and probably heartburn from pizza, has been known to make even the most level-headed systems administrators behave irrationally.</TABLE>
<H4 ALIGN="LEFT"><A NAME="Heading5"></A><FONT COLOR="#000077">Listing Services by Using <I>chkconfig</I>
</FONT></H4>
<P>To get a list of which services are started at which run level, use the command <TT>chkconfig --list</TT>. Optionally, you can add a name as an additional argument, and <TT>chkconfig</TT> will list only the information for that service. Following is the output of <TT>chkconfig—list httpd</TT> on my system:</P>
<!-- CODE SNIP //-->
<PRE>
[jwalter@jansmachine jwalter]$ /sbin/chkconfig --list httpd
httpd 0:off 1:off 2:off 3:on 4:on 5:on 6:off
</PRE>
<!-- END CODE SNIP //-->
<P>In this case, <TT>chkconfig</TT> reports that the <TT>httpd</TT> service is to be started for run levels 3, 4, and 5.</P>
<TABLE BORDER="2" BORDERCOLOR="#0000" ALIGN="CENTER">
<TR><TD><FONT SIZE="+1"><B>More uses for chkconfig</B></FONT>
<BR>The <TT>chkconfig</TT> program also provides a means to add and remove services from run levels from the shell command line without having to manipulate symbolic links.</TABLE>
<H4 ALIGN="LEFT"><A NAME="Heading6"></A><FONT COLOR="#000077">Removing a Service Using <I>chkconfig</I>
</FONT></H4>
<P>To remove a service from a certain run level, use the following command:
</P>
<!-- CODE SNIP //-->
<PRE>
chkconfig --level <<I>runlevel</I>> <<I>servicename</I>> off
</PRE>
<!-- END CODE SNIP //-->
<P><TT><I>runlevel</I></TT> is the run level number you want to modify, and <TT><I>servicename</I></TT> is the name of the service. The commands <TT>on</TT> and <TT>off</TT> enable and disable the service, respectively. For example, to turn off <TT>httpd</TT> for run level 3, you would issue the following command:</P>
<!-- CODE SNIP //-->
<PRE>
chkconfig --level 3 httpd off
</PRE>
<!-- END CODE SNIP //-->
<P>This command removes the service only for a specific run level, which is the most common operation. You can list multiple run levels (for example, you could type <TT>345</TT> on the command line to change the settings for multiple run levels). If you need to remove a service altogether, you would use the following command:</P>
<!-- CODE SNIP //-->
<PRE>
chkconfig --del httpd
</PRE>
<!-- END CODE SNIP //-->
<P>This command has the effect of removing <TT>httpd</TT> from all <TT>rcX.d</TT> directories. It does not remove the service’s script from the <TT>/etc/rc.d/init.d</TT> directory; it just removes the symbolic links to the file from the directories that the <TT>init</TT> scripts search.</P>
<TABLE BORDER="2" BORDERCOLOR="#0000" ALIGN="CENTER">
<TR><TD><FONT SIZE="+1"><B>Stopping the service before removing it</B></FONT>
<BR>When you remove or disable a service, you have no guarantee that the system will stop the service gracefully the next time the system changes run levels. To make sure a service is stopped properly, do it manually before removing the service from the run levels.
<P>You can use <TT>tksysv</TT> or <TT>ntsysv</TT> to manually stop a service—these are graphical front ends to manage the services on the system for X11 and text mode, respectively.</P>
<P>You can also simply call the script that manages the service by employing the <TT>stop</TT> command-line argument. For example, you would use the following command to stop <TT>httpd</TT> before disabling it:</P>
<!-- CODE SNIP //-->
<PRE>
/etc/rc.d/init.d/httpd
stop
</PRE>
<!-- END CODE SNIP //-->
<P>Incidentally, you can use this method to start services outside of their normal run level or before adding them by using the <TT>start</TT> command line argument instead. This is useful for determining whether the services are running properly.</TABLE>
</P>
<H4 ALIGN="LEFT"><A NAME="Heading7"></A><FONT COLOR="#000077">Adding a Service by Using <I>chkconfig</I>
</FONT></H4>
<P>To add a service to a specific run level, use the following command:
</P>
<!-- CODE SNIP //-->
<PRE>
chkconfig --level <<I>runlevel</I>> <<I>servicename</I>> on
</PRE>
<!-- END CODE SNIP //-->
<P>To add <TT>httpd</TT> back to run level 3, use the following command:</P>
<!-- CODE SNIP //-->
<PRE>
chkconfig --level 3 httpd on
</PRE>
<!-- END CODE SNIP //-->
<P>To add a new service to all run levels according to the recommendations given to <TT>chkconfig</TT>, use the following command:</P>
<!-- CODE SNIP //-->
<PRE>
chkconfig --add <<I>servicename</I>>
</PRE>
<!-- END CODE SNIP //-->
<P><TT>chkconfig</TT> sets all the links for the service in the correct directories in one swoop.</P>
<H4 ALIGN="LEFT"><A NAME="Heading8"></A><FONT COLOR="#000077">Resetting Service Information</FONT></H4>
<P>Playing with services is educational, as long as you have a backup of your <TT>/etc/rc.d</TT> directory tree and a way to get back into the system to restore it. However, this type of drastic action is usually not necessary. Instead, you can restore the service’s startup priority and other information to the recommended settings by issuing the following command</P>
<!-- CODE SNIP //-->
<PRE>
chkconfig <<I>servicename</I>> reset
</PRE>
<!-- END CODE SNIP //-->
<P>This command returns everything to a (hopefully) “sane” default.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="470-472.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="474-478.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 + -