]> granicus.if.org Git - apache/blob - docs/manual/stopping.xml
use <program> for programs
[apache] / docs / manual / stopping.xml
1 <?xml version='1.0' encoding='UTF-8' ?>
2 <!DOCTYPE manualpage SYSTEM "./style/manualpage.dtd">
3 <?xml-stylesheet type="text/xsl" href="./style/manual.en.xsl"?>
4 <!-- $LastChangedRevision$ -->
5
6 <!--
7  Copyright 2002-2004 The Apache Software Foundation
8
9  Licensed under the Apache License, Version 2.0 (the "License");
10  you may not use this file except in compliance with the License.
11  You may obtain a copy of the License at
12
13      http://www.apache.org/licenses/LICENSE-2.0
14
15  Unless required by applicable law or agreed to in writing, software
16  distributed under the License is distributed on an "AS IS" BASIS,
17  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  See the License for the specific language governing permissions and
19  limitations under the License.
20 -->
21
22 <manualpage metafile="stopping.xml.meta">
23
24   <title>Stopping and Restarting</title>
25
26 <summary>
27     <p>This document covers stopping and restarting Apache on
28     Unix-like systems. Windows NT, 2000 and XP users should see
29     <a href="platform/windows.html#winsvc">Running Apache as a
30     Service</a> and Windows 9x and ME users should see <a
31     href="platform/windows.html#wincons">Running Apache as a
32     Console Application</a> for information on how to control
33     Apache on those platforms.</p>
34 </summary>
35
36 <seealso><program>httpd</program></seealso>
37 <seealso><program>apachectl</program></seealso>
38
39 <section id="introduction"><title>Introduction</title>
40
41     <p>In order to stop or restart Apache, you must send a signal to
42     the running <program>httpd</program> processes.  There are two ways to
43     send the signals.  First, you can use the unix <code>kill</code>
44     command to directly send signals to the processes. You will
45     notice many <program>httpd</program> executables running on your system,
46     but you should not send signals to any of them except the parent,
47     whose pid is in the <directive
48     module="mpm_common">PidFile</directive>. That is to say you
49     shouldn't ever need to send signals to any process except the
50     parent. There are three signals that you can send the parent:
51     <code><a href="#term">TERM</a></code>,
52     <code><a href="#hup">HUP</a></code>, and
53     <code><a href="#graceful">USR1</a></code>, which
54     will be described in a moment.</p>
55
56     <p>To send a signal to the parent you should issue a command
57     such as:</p>
58
59 <example>kill -TERM `cat /usr/local/apache2/logs/httpd.pid`</example>
60
61     <p>The second method of signaling the <program>httpd</program> processes
62     is to use the <code>-k</code> command line options: <code>stop</code>,
63     <code>restart</code>, and <code>graceful</code>,
64     as described below.  These are arguments to the <program>
65     httpd</program> binary, but we recommend that
66     you send them using the <program>apachectl</program> control script, which
67     will pass them through to <program>httpd</program>.</p>
68
69     <p>After you have signaled <program>httpd</program>, you can read about
70     its progress by issuing:</p>
71
72 <example>tail -f /usr/local/apache2/logs/error_log</example>
73
74     <p>Modify those examples to match your <directive
75     module="core">ServerRoot</directive> and <directive
76     module="mpm_common">PidFile</directive> settings.</p>
77 </section>
78
79 <section id="term"><title>Stop Now</title>
80
81 <dl><dt>Signal: TERM</dt>
82 <dd><code>apachectl -k stop</code></dd>
83 </dl>
84
85     <p>Sending the <code>TERM</code> or <code>stop</code> signal to
86     the parent causes it to immediately attempt to kill off all of its
87     children. It may take it several seconds to complete killing off
88     its children.  Then the parent itself exits. Any requests in
89     progress are terminated, and no further requests are served.</p>
90 </section>
91
92 <section id="graceful"><title>Graceful Restart</title>
93
94 <dl><dt>Signal: USR1</dt>
95 <dd><code>apachectl -k graceful</code></dd>
96 </dl>
97
98     <p>The <code>USR1</code> or <code>graceful</code> signal causes
99     the parent process to <em>advise</em> the children to exit after
100     their current request (or to exit immediately if they're not
101     serving anything). The parent re-reads its configuration files and
102     re-opens its log files. As each child dies off the parent replaces
103     it with a child from the new <em>generation</em> of the
104     configuration, which begins serving new requests immediately.</p>
105
106     <note>On certain platforms that do not allow <code>USR1</code> to
107     be used for a graceful restart, an alternative signal may be used (such
108     as <code>WINCH</code>). The command <code>apachectl graceful</code>
109     will send the right signal for your platform.</note>
110
111     <p>This code is designed to always respect the process control
112     directive of the MPMs, so the number of processes and threads
113     available to serve clients will be maintained at the appropriate
114     values throughout the restart process.  Furthermore, it respects
115     <directive module="mpm_common">StartServers</directive> in the
116     following manner: if after one second at least <directive
117     module="mpm_common">StartServers</directive> new children have not
118     been created, then create enough to pick up the slack. Hence the
119     code tries to maintain both the number of children appropriate for
120     the current load on the server, and respect your wishes with the
121     <directive>StartServers</directive> parameter.</p>
122
123     <p>Users of the <module>mod_status</module>
124     will notice that the server statistics are <strong>not</strong>
125     set to zero when a <code>USR1</code> is sent. The code was
126     written to both minimize the time in which the server is unable
127     to serve new requests (they will be queued up by the operating
128     system, so they're not lost in any event) and to respect your
129     tuning parameters. In order to do this it has to keep the
130     <em>scoreboard</em> used to keep track of all children across
131     generations.</p>
132
133     <p>The status module will also use a <code>G</code> to indicate
134     those children which are still serving requests started before
135     the graceful restart was given.</p>
136
137     <p>At present there is no way for a log rotation script using
138     <code>USR1</code> to know for certain that all children writing
139     the pre-restart log have finished. We suggest that you use a
140     suitable delay after sending the <code>USR1</code> signal
141     before you do anything with the old log. For example if most of
142     your hits take less than 10 minutes to complete for users on
143     low bandwidth links then you could wait 15 minutes before doing
144     anything with the old log.</p>
145
146     <note>If your configuration file has errors
147     in it when you issue a restart then your parent will not
148     restart, it will exit with an error. In the case of graceful
149     restarts it will also leave children running when it exits.
150     (These are the children which are "gracefully exiting" by
151     handling their last request.) This will cause problems if you
152     attempt to restart the server -- it will not be able to bind to
153     its listening ports. Before doing a restart, you can check the
154     syntax of the configuration files with the <code>-t</code>
155     command line argument (see <program>httpd</program>). This still will not
156     guarantee that the server will restart correctly. To check the
157     semantics of the configuration files as well as the syntax, you
158     can try starting <program>httpd</program> as a non-root user. If there
159     are no errors it will attempt to open its sockets and logs and fail
160     because it's not root (or because the currently running
161     <program>httpd</program> already has those ports bound). If it fails
162     for any other reason then it's probably a config file error and the error
163     should be fixed before issuing the graceful restart.</note>
164 </section>
165
166 <section id="hup"><title>Restart Now</title>
167
168 <dl><dt>Signal: HUP</dt>
169 <dd><code>apachectl -k restart</code></dd>
170 </dl>
171
172     <p>Sending the <code>HUP</code> or <code>restart</code> signal to
173     the parent causes it to kill off its children like in
174     <code>TERM</code>, but the parent doesn't exit. It re-reads its
175     configuration files, and re-opens any log files. Then it spawns a
176     new set of children and continues serving hits.</p>
177
178     <p>Users of <module>mod_status</module>
179     will notice that the server statistics are set to zero when a
180     <code>HUP</code> is sent.</p>
181
182 <note>If your configuration file has errors in it when you issue a
183 restart then your parent will not restart, it will exit with an
184 error. See above for a method of avoiding this.</note>
185 </section>
186
187 <section id="race"><title>Appendix: signals and race conditions</title>
188
189     <p>Prior to Apache 1.2b9 there were several <em>race
190     conditions</em> involving the restart and die signals (a simple
191     description of race condition is: a time-sensitive problem, as
192     in if something happens at just the wrong time it won't behave
193     as expected). For those architectures that have the "right"
194     feature set we have eliminated as many as we can. But it should
195     be noted that there still do exist race conditions on certain
196     architectures.</p>
197
198     <p>Architectures that use an on disk <directive
199     module="mpm_common">ScoreBoardFile</directive> have the potential
200     to corrupt their scoreboards. This can result in the "bind:
201     Address already in use" (after <code>HUP</code>) or "long lost
202     child came home!" (after <code>USR1</code>). The former is a fatal
203     error, while the latter just causes the server to lose a
204     scoreboard slot. So it might be advisable to use graceful
205     restarts, with an occasional hard restart. These problems are very
206     difficult to work around, but fortunately most architectures do
207     not require a scoreboard file. See the <directive
208     module="mpm_common">ScoreBoardFile</directive> documentation for a
209     architecture uses it.</p>
210
211     <p>All architectures have a small race condition in each child
212     involving the second and subsequent requests on a persistent
213     HTTP connection (KeepAlive). It may exit after reading the
214     request line but before reading any of the request headers.
215     There is a fix that was discovered too late to make 1.2. In
216     theory this isn't an issue because the KeepAlive client has to
217     expect these events because of network latencies and server
218     timeouts. In practice it doesn't seem to affect anything either
219     -- in a test case the server was restarted twenty times per
220     second and clients successfully browsed the site without
221     getting broken images or empty documents. </p>
222 </section>
223
224 </manualpage>