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$ -->
7 Licensed to the Apache Software Foundation (ASF) under one or more
8 contributor license agreements. See the NOTICE file distributed with
9 this work for additional information regarding copyright ownership.
10 The ASF licenses this file to You under the Apache License, Version 2.0
11 (the "License"); you may not use this file except in compliance with
12 the License. You may obtain a copy of the License at
14 http://www.apache.org/licenses/LICENSE-2.0
16 Unless required by applicable law or agreed to in writing, software
17 distributed under the License is distributed on an "AS IS" BASIS,
18 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 See the License for the specific language governing permissions and
20 limitations under the License.
23 <manualpage metafile="stopping.xml.meta">
25 <title>Stopping and Restarting</title>
28 <p>This document covers stopping and restarting Apache on
29 Unix-like systems. Windows NT, 2000 and XP users should see
30 <a href="platform/windows.html#winsvc">Running Apache as a
31 Service</a> and Windows 9x and ME users should see <a
32 href="platform/windows.html#wincons">Running Apache as a
33 Console Application</a> for information on how to control
34 Apache on those platforms.</p>
37 <seealso><program>httpd</program></seealso>
38 <seealso><program>apachectl</program></seealso>
39 <seealso><a href="invoking.html">Starting</a></seealso>
41 <section id="introduction"><title>Introduction</title>
43 <p>In order to stop or restart Apache, you must send a signal to
44 the running <program>httpd</program> processes. There are two ways to
45 send the signals. First, you can use the unix <code>kill</code>
46 command to directly send signals to the processes. You will
47 notice many <program>httpd</program> executables running on your system,
48 but you should not send signals to any of them except the parent,
49 whose pid is in the <directive
50 module="mpm_common">PidFile</directive>. That is to say you
51 shouldn't ever need to send signals to any process except the
52 parent. There are four signals that you can send the parent:
53 <code><a href="#term">TERM</a></code>,
54 <code><a href="#graceful">USR1</a></code>,
55 <code><a href="#hup">HUP</a></code>, and
56 <code><a href="#gracefulstop">WINCH</a></code>, which
57 will be described in a moment.</p>
59 <p>To send a signal to the parent you should issue a command
62 <example>kill -TERM `cat /usr/local/apache2/logs/httpd.pid`</example>
64 <p>The second method of signaling the <program>httpd</program> processes
65 is to use the <code>-k</code> command line options: <code>stop</code>,
66 <code>restart</code>, <code>graceful</code> and <code>graceful-stop</code>,
67 as described below. These are arguments to the <program>
68 httpd</program> binary, but we recommend that
69 you send them using the <program>apachectl</program> control script, which
70 will pass them through to <program>httpd</program>.</p>
72 <p>After you have signaled <program>httpd</program>, you can read about
73 its progress by issuing:</p>
75 <example>tail -f /usr/local/apache2/logs/error_log</example>
77 <p>Modify those examples to match your <directive
78 module="core">ServerRoot</directive> and <directive
79 module="mpm_common">PidFile</directive> settings.</p>
82 <section id="term"><title>Stop Now</title>
84 <dl><dt>Signal: TERM</dt>
85 <dd><code>apachectl -k stop</code></dd>
88 <p>Sending the <code>TERM</code> or <code>stop</code> signal to
89 the parent causes it to immediately attempt to kill off all of its
90 children. It may take it several seconds to complete killing off
91 its children. Then the parent itself exits. Any requests in
92 progress are terminated, and no further requests are served.</p>
95 <section id="graceful"><title>Graceful Restart</title>
97 <dl><dt>Signal: USR1</dt>
98 <dd><code>apachectl -k graceful</code></dd>
101 <p>The <code>USR1</code> or <code>graceful</code> signal causes
102 the parent process to <em>advise</em> the children to exit after
103 their current request (or to exit immediately if they're not
104 serving anything). The parent re-reads its configuration files and
105 re-opens its log files. As each child dies off the parent replaces
106 it with a child from the new <em>generation</em> of the
107 configuration, which begins serving new requests immediately.</p>
109 <p>This code is designed to always respect the process control
110 directive of the MPMs, so the number of processes and threads
111 available to serve clients will be maintained at the appropriate
112 values throughout the restart process. Furthermore, it respects
113 <directive module="mpm_common">StartServers</directive> in the
114 following manner: if after one second at least <directive
115 module="mpm_common">StartServers</directive> new children have not
116 been created, then create enough to pick up the slack. Hence the
117 code tries to maintain both the number of children appropriate for
118 the current load on the server, and respect your wishes with the
119 <directive module="mpm_common">StartServers</directive>
122 <p>Users of <module>mod_status</module>
123 will notice that the server statistics are <strong>not</strong>
124 set to zero when a <code>USR1</code> is sent. The code was
125 written to both minimize the time in which the server is unable
126 to serve new requests (they will be queued up by the operating
127 system, so they're not lost in any event) and to respect your
128 tuning parameters. In order to do this it has to keep the
129 <em>scoreboard</em> used to keep track of all children across
132 <p>The status module will also use a <code>G</code> to indicate
133 those children which are still serving requests started before
134 the graceful restart was given.</p>
136 <p>At present there is no way for a log rotation script using
137 <code>USR1</code> to know for certain that all children writing
138 the pre-restart log have finished. We suggest that you use a
139 suitable delay after sending the <code>USR1</code> signal
140 before you do anything with the old log. For example if most of
141 your hits take less than 10 minutes to complete for users on
142 low bandwidth links then you could wait 15 minutes before doing
143 anything with the old log.</p>
146 <p>When you issue a restart, a syntax check is first run, to
147 ensure that there are no errors in the configuration files.
148 If your configuration file has errors in it, you will get an
149 error message about that syntax error, and the server will refuse to
150 restart. This avoids the situation where the server halts and then
151 cannot restart, leaving you with a non-functioning server.</p>
153 <p>This still will not
154 guarantee that the server will restart correctly. To check the
155 semantics of the configuration files as well as the syntax, you
156 can try starting <program>httpd</program> as a non-root user. If there
157 are no errors it will attempt to open its sockets and logs and fail
158 because it's not root (or because the currently running
159 <program>httpd</program> already has those ports bound). If it fails
160 for any other reason then it's probably a config file error and the error
161 should be fixed before issuing the graceful restart.</p></note>
164 <section id="hup"><title>Restart Now</title>
166 <dl><dt>Signal: HUP</dt>
167 <dd><code>apachectl -k restart</code></dd>
170 <p>Sending the <code>HUP</code> or <code>restart</code> signal to
171 the parent causes it to kill off its children like in
172 <code>TERM</code>, but the parent doesn't exit. It re-reads its
173 configuration files, and re-opens any log files. Then it spawns a
174 new set of children and continues serving hits.</p>
176 <p>Users of <module>mod_status</module>
177 will notice that the server statistics are set to zero when a
178 <code>HUP</code> is sent.</p>
180 <note>As with a graceful restart, a syntax check is run before the the
181 restart is attempted. If your configuration file has errors in it, the
182 restart will not be attempted, and you will receive notification of the
183 syntax error(s).</note>
186 <section id="gracefulstop"><title>Graceful Stop</title>
188 <dl><dt>Signal: WINCH</dt>
189 <dd><code>apachectl -k graceful-stop</code></dd>
192 <p>The <code>WINCH</code> or <code>graceful-stop</code> signal causes
193 the parent process to <em>advise</em> the children to exit after
194 their current request (or to exit immediately if they're not
195 serving anything). The parent will then remove its <directive
196 module="mpm_common">PidFile</directive> and cease listening on
197 all ports. The parent will continue to run, and monitor children
198 which are handling requests. Once all children have finalised
199 and exited or the timeout specified by the <directive
200 module="mpm_common">GracefulShutdownTimeout</directive> has been
201 reached, the parent will also exit. If the timeout is reached,
202 any remaining children will be sent the <code>TERM</code> signal
203 to force them to exit.</p>
205 <p>A <code>TERM</code> signal will immediately terminate the
206 parent process and all children when in the "graceful" state. However
207 as the <directive module="mpm_common">PidFile</directive> will
208 have been removed, you will not be able to use
209 <code>apachectl</code> or <code>httpd</code> to send this signal,</p>
211 <note><p>The <code>graceful-stop</code> signal allows you to run multiple
212 identically configured instances of <program>httpd</program> at the
213 same time. This is a powerful feature when performing graceful
214 upgrades of Apache, however it can also cause deadlocks and race
215 conditions with some configurations.</p>
217 <p>Care has been taken to ensure that on-disk files
218 such as the <directive module="core">Lockfile</directive> and <directive
219 module="mod_cgid">ScriptSock</directive> files contain the server
220 PID, and should co-exist without problem. However, if a configuration
221 directive, third-party module or persistent CGI utilises any other on-disk
222 lock or state files; care should be taken to ensure that multiple running
223 instances of <program>httpd</program> do not clobber each others files.</p>
225 <p>You should also be wary of other potential race conditions, such as
226 using <program>rotatelogs</program> style piped logging. Multiple running
227 instances of <program>rotatelogs</program> attempting to rotate the same
228 logfiles at the same time may destroy each other's logfiles.</p></note>