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"?>
5 <relativepath href="."/>
7 <title>Stopping and Restarting</title>
10 <p>This document covers stopping and restarting Apache on
11 Unix-like systems. Windows users should see <a
12 href="platform/windows.html#signal">Signaling Apache when
16 <seealso><a href="programs/httpd.html">httpd</a></seealso>
17 <seealso><a href="programs/apachectl.html">apachectl</a></seealso>
19 <section id="introduction"><title>Introduction</title>
21 <p>In order to stop or restart Apache, you must send a signal to
22 the running <code>httpd</code> processes. There are two ways to
23 send the signals. First, you can use the unix <code>kill</code>
24 command to directly send signals to the processes. You will
25 notice many <code>httpd</code> executables running on your system,
26 but you should not send signals to any of them except the parent,
27 whose pid is in the <directive
28 module="mpm_common">PidFile</directive>. That is to say you
29 shouldn't ever need to send signals to any process except the
30 parent. There are three signals that you can send the parent:
31 <code>TERM</code>, <code>HUP</code>, and <code>USR1</code>, which
32 will be described in a moment.</p>
34 <p>To send a signal to the parent you should issue a command
37 <example>kill -TERM `cat /usr/local/apache2/logs/httpd.pid`</example>
39 <p>The second method of signaling the <code>httpd</code> processes
40 is to use the <code>-k</code> command line options: stop, restart,
41 and graceful, as described below. These are arguments to the <a
42 href="programs/httpd.html">httpd</a> binary, but we recommend that
43 you send them using the <a
44 href="programs/apachectl.html">apachectl</a> control script, which
45 will pass them through to <code>httpd</code>.</p>
47 <p>After you have signaled <code>httpd</code>, you can read about
48 its progress by issuing:</p>
50 <example>tail -f /usr/local/apache2/logs/error_log</example>
52 <p>Modify those examples to match your <directive
53 module="core">ServerRoot</directive> and <directive
54 module="mpm_common">PidFile</directive> settings.</p>
57 <section id="term"><title>Stop Now</title>
59 <dl><dt>Signal: TERM</dt>
60 <dd><code>apachectl -k stop</code></dd>
63 <p>Sending the <code>TERM</code> or <code>stop</code> signal to
64 the parent causes it to immediately attempt to kill off all of its
65 children. It may take it several seconds to complete killing off
66 its children. Then the parent itself exits. Any requests in
67 progress are terminated, and no further requests are served.</p>
70 <section id="graceful"><title>Graceful Restart</title>
72 <dl><dt>Signal: USR1</dt>
73 <dd><code>apachectl -k graceful</code></dd>
76 <p>The <code>USR1</code> or <code>graceful</code> signal causes
77 the parent process to <em>advise</em> the children to exit after
78 their current request (or to exit immediately if they're not
79 serving anything). The parent re-reads its configuration files and
80 re-opens its log files. As each child dies off the parent replaces
81 it with a child from the new <em>generation</em> of the
82 configuration, which begins serving new requests immediately.</p>
84 <note>On certain platforms that do not allow USR1 to be used for a
85 graceful restart, an alternative signal may be used (such as
86 WINCH). The command <code>apachectl graceful</code> will send the
87 right signal for your platform.</note>
89 <p>This code is designed to always respect the process control
90 directive of the MPMs, so the number of processes and threads
91 available to serve clients will be maintained at the appropriate
92 values throughout the restart process. Furthermore, it respects
93 <directive module="mpm_common">StartServers</directive> in the
94 following manner: if after one second at least <directive
95 module="mpm_common">StartServers</directive> new children have not
96 been created, then create enough to pick up the slack. Hence the
97 code tries to maintain both the number of children appropriate for
98 the current load on the server, and respect your wishes with the
99 StartServers parameter.</p>
101 <p>Users of the <module>mod_status</module>
102 will notice that the server statistics are <strong>not</strong>
103 set to zero when a <code>USR1</code> is sent. The code was
104 written to both minimize the time in which the server is unable
105 to serve new requests (they will be queued up by the operating
106 system, so they're not lost in any event) and to respect your
107 tuning parameters. In order to do this it has to keep the
108 <em>scoreboard</em> used to keep track of all children across
111 <p>The status module will also use a <code>G</code> to indicate
112 those children which are still serving requests started before
113 the graceful restart was given.</p>
115 <p>At present there is no way for a log rotation script using
116 <code>USR1</code> to know for certain that all children writing
117 the pre-restart log have finished. We suggest that you use a
118 suitable delay after sending the <code>USR1</code> signal
119 before you do anything with the old log. For example if most of
120 your hits take less than 10 minutes to complete for users on
121 low bandwidth links then you could wait 15 minutes before doing
122 anything with the old log.</p>
124 <note>If your configuration file has errors
125 in it when you issue a restart then your parent will not
126 restart, it will exit with an error. In the case of graceful
127 restarts it will also leave children running when it exits.
128 (These are the children which are "gracefully exiting" by
129 handling their last request.) This will cause problems if you
130 attempt to restart the server -- it will not be able to bind to
131 its listening ports. Before doing a restart, you can check the
132 syntax of the configuration files with the <code>-t</code>
133 command line argument (see <a
134 href="programs/httpd.html">httpd</a>). This still will not
135 guarantee that the server will restart correctly. To check the
136 semantics of the configuration files as well as the syntax, you
137 can try starting httpd as a non-root user. If there are no
138 errors it will attempt to open its sockets and logs and fail
139 because it's not root (or because the currently running httpd
140 already has those ports bound). If it fails for any other
141 reason then it's probably a config file error and the error
142 should be fixed before issuing the graceful restart.</note>
145 <section id="hup"><title>Restart Now</title>
147 <dl><dt>Signal: HUP</dt>
148 <dd><code>apachectl -k restart</code></dd>
151 <p>Sending the <code>HUP</code> or <code>restart</code> signal to
152 the parent causes it to kill off its children like in
153 <code>TERM</code>, but the parent doesn't exit. It re-reads its
154 configuration files, and re-opens any log files. Then it spawns a
155 new set of children and continues serving hits.</p>
157 <p>Users of <module>mod_status</module>
158 will notice that the server statistics are set to zero when a
159 <code>HUP</code> is sent.</p>
161 <note>If your configuration file has errors in it when you issue a
162 restart then your parent will not restart, it will exit with an
163 error. See above for a method of avoiding this.</note>
166 <section id="race"><title>Appendix: signals and race conditions</title>
168 <p>Prior to Apache 1.2b9 there were several <em>race
169 conditions</em> involving the restart and die signals (a simple
170 description of race condition is: a time-sensitive problem, as
171 in if something happens at just the wrong time it won't behave
172 as expected). For those architectures that have the "right"
173 feature set we have eliminated as many as we can. But it should
174 be noted that there still do exist race conditions on certain
177 <p>Architectures that use an on disk <directive
178 module="mpm_common">ScoreBoardFile</directive> have the potential
179 to corrupt their scoreboards. This can result in the "bind:
180 Address already in use" (after <code>HUP</code>) or "long lost
181 child came home!" (after <code>USR1</code>). The former is a fatal
182 error, while the latter just causes the server to lose a
183 scoreboard slot. So it might be advisable to use graceful
184 restarts, with an occasional hard restart. These problems are very
185 difficult to work around, but fortunately most architectures do
186 not require a scoreboard file. See the <directive
187 module="mpm_common">ScoreBoardFile</directive> documentation for a
188 architecture uses it.</p>
190 <p>All architectures have a small race condition in each child
191 involving the second and subsequent requests on a persistent
192 HTTP connection (KeepAlive). It may exit after reading the
193 request line but before reading any of the request headers.
194 There is a fix that was discovered too late to make 1.2. In
195 theory this isn't an issue because the KeepAlive client has to
196 expect these events because of network latencies and server
197 timeouts. In practice it doesn't seem to affect anything either
198 -- in a test case the server was restarted twenty times per
199 second and clients successfully browsed the site without
200 getting broken images or empty documents. </p>