1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
6<html xmlns="http://www.w3.org/1999/xhtml">
7  <head>
8    <meta name="generator" content="HTML Tidy, see www.w3.org" />
9
10    <title>Stopping and Restarting Apache</title>
11  </head>
12  <!-- Background white, links blue (unvisited), navy (visited), red (active) -->
13
14  <body bgcolor="#FFFFFF" text="#000000" link="#0000FF"
15  vlink="#000080" alink="#FF0000">
16        <div align="CENTER">
17      <img src="images/sub.gif" alt="[APACHE DOCUMENTATION]" />
18
19      <h3>Apache HTTP Server</h3>
20    </div>
21
22
23
24    <h1 align="CENTER">Stopping and Restarting Apache</h1>
25
26    <p>You will notice many <code>httpd</code> executables running
27    on your system, but you should not send signals to any of them
28    except the parent, whose pid is in the <a
29    href="mod/core.html#pidfile">PidFile</a>. That is to say you
30    shouldn't ever need to send signals to any process except the
31    parent. There are three signals that you can send the parent:
32    <code>TERM</code>, <code>HUP</code>, and <code>USR1</code>,
33    which will be described in a moment.</p>
34
35    <p>To send a signal to the parent you should issue a command
36    such as:</p>
37
38    <blockquote>
39<pre>
40    kill -TERM `cat /usr/local/apache/logs/httpd.pid`
41</pre>
42    </blockquote>
43    You can read about its progress by issuing:
44
45    <blockquote>
46<pre>
47    tail -f /usr/local/apache/logs/error_log
48</pre>
49    </blockquote>
50    Modify those examples to match your <a
51    href="mod/core.html#serverroot">ServerRoot</a> and <a
52    href="mod/core.html#pidfile">PidFile</a> settings.
53
54    <p>As of Apache 1.3 we provide a script called <a
55    href="programs/apachectl.html">apachectl</a> which can be used
56    to start, stop, and restart Apache. It may need a little
57    customization for your system, see the comments at the top of
58    the script.</p>
59
60    <h3>TERM Signal: stop now</h3>
61
62    <p>Sending the <code>TERM</code> signal to the parent causes it
63    to immediately attempt to kill off all of its children. It may
64    take it several seconds to complete killing off its children.
65    Then the parent itself exits. Any requests in progress are
66    terminated, and no further requests are served.</p>
67
68    <h3>HUP Signal: restart now</h3>
69
70    <p>Sending the <code>HUP</code> signal to the parent causes it
71    to kill off its children like in <code>TERM</code> but the
72    parent doesn't exit. It re-reads its configuration files, and
73    re-opens any log files. Then it spawns a new set of children
74    and continues serving hits.</p>
75
76    <p>Users of the <a href="mod/mod_status.html">status module</a>
77    will notice that the server statistics are set to zero when a
78    <code>HUP</code> is sent.</p>
79
80    <p><strong>Note:</strong> If your configuration file has errors
81    in it when you issue a restart then your parent will not
82    restart, it will exit with an error. See below for a method of
83    avoiding this.</p>
84
85    <h3>USR1 Signal: graceful restart</h3>
86
87    <p><strong>Note:</strong> prior to release 1.2b9 this code is
88    quite unstable and shouldn't be used at all.</p>
89
90    <p>The <code>USR1</code> signal causes the parent process to
91    <em>advise</em> the children to exit after their current
92    request (or to exit immediately if they're not serving
93    anything). The parent re-reads its configuration files and
94    re-opens its log files. As each child dies off the parent
95    replaces it with a child from the new <em>generation</em> of
96    the configuration, which begins serving new requests
97    immediately.</p>
98
99    <p>This code is designed to always respect the <a
100    href="mod/core.html#maxclients">MaxClients</a>, <a
101    href="mod/core.html#minspareservers">MinSpareServers</a>, and
102    <a href="mod/core.html#maxspareservers">MaxSpareServers</a>
103    settings. Furthermore, it respects <a
104    href="mod/core.html#startservers">StartServers</a> in the
105    following manner: if after one second at least StartServers new
106    children have not been created, then create enough to pick up
107    the slack. This is to say that the code tries to maintain both
108    the number of children appropriate for the current load on the
109    server, and respect your wishes with the StartServers
110    parameter.</p>
111
112    <p>Users of the <a href="mod/mod_status.html">status module</a>
113    will notice that the server statistics are <strong>not</strong>
114    set to zero when a <code>USR1</code> is sent. The code was
115    written to both minimize the time in which the server is unable
116    to serve new requests (they will be queued up by the operating
117    system, so they're not lost in any event) and to respect your
118    tuning parameters. In order to do this it has to keep the
119    <em>scoreboard</em> used to keep track of all children across
120    generations.</p>
121
122    <p>The status module will also use a <code>G</code> to indicate
123    those children which are still serving requests started before
124    the graceful restart was given.</p>
125
126    <p>At present there is no way for a log rotation script using
127    <code>USR1</code> to know for certain that all children writing
128    the pre-restart log have finished. We suggest that you use a
129    suitable delay after sending the <code>USR1</code> signal
130    before you do anything with the old log. For example if most of
131    your hits take less than 10 minutes to complete for users on
132    low bandwidth links then you could wait 15 minutes before doing
133    anything with the old log.</p>
134
135    <p><strong>Note:</strong> If your configuration file has errors
136    in it when you issue a restart then your parent will not
137    restart, it will exit with an error. In the case of graceful
138    restarts it will also leave children running when it exits.
139    (These are the children which are "gracefully exiting" by
140    handling their last request.) This will cause problems if you
141    attempt to restart the server -- it will not be able to bind to
142    its listening ports. Before doing a restart, you can check the
143    syntax of the configuration files with the <code>-t</code>
144    command line argument (see <a
145    href="programs/httpd.html">httpd</a> ). This still will not
146    guarantee that the server will restart correctly. To check the
147    semantics of the configuration files as well as the syntax, you
148    can try starting httpd as a non-root user. If there are no
149    errors it will attempt to open its sockets and logs and fail
150    because it's not root (or because the currently running httpd
151    already has those ports bound). If it fails for any other
152    reason then it's probably a config file error and the error
153    should be fixed before issuing the graceful restart.</p>
154
155    <h3>Appendix: signals and race conditions</h3>
156
157    <p>Prior to Apache 1.2b9 there were several <em>race
158    conditions</em> involving the restart and die signals (a simple
159    description of race condition is: a time-sensitive problem, as
160    in if something happens at just the wrong time it won't behave
161    as expected). For those architectures that have the "right"
162    feature set we have eliminated as many as we can. But it should
163    be noted that there still do exist race conditions on certain
164    architectures.</p>
165
166    <p>Architectures that use an on disk <a
167    href="mod/core.html#scoreboardfile">ScoreBoardFile</a> have the
168    potential to corrupt their scoreboards. This can result in the
169    "bind: Address already in use" (after <code>HUP</code>) or
170    "long lost child came home!" (after <code>USR1</code>). The
171    former is a fatal error, while the latter just causes the
172    server to lose a scoreboard slot. So it might be advisable to
173    use graceful restarts, with an occasional hard restart. These
174    problems are very difficult to work around, but fortunately
175    most architectures do not require a scoreboard file. See the <a
176    href="mod/core.html#scoreboardfile">ScoreBoardFile</a>
177    documentation for a architecture uses it.</p>
178
179    <p><code>NEXT</code> and <code>MACHTEN</code> (68k only) have
180    small race conditions which can cause a restart/die signal to
181    be lost, but should not cause the server to do anything
182    otherwise problematic.
183    <!-- they don't have sigaction, or we're not using it -djg -->
184    </p>
185
186    <p>All architectures have a small race condition in each child
187    involving the second and subsequent requests on a persistent
188    HTTP connection (KeepAlive). It may exit after reading the
189    request line but before reading any of the request headers.
190    There is a fix that was discovered too late to make 1.2. In
191    theory this isn't an issue because the KeepAlive client has to
192    expect these events because of network latencies and server
193    timeouts. In practice it doesn't seem to affect anything either
194    -- in a test case the server was restarted twenty times per
195    second and clients successfully browsed the site without
196    getting broken images or empty documents.
197        <hr />
198
199    <h3 align="CENTER">Apache HTTP Server</h3>
200    <a href="./"><img src="images/index.gif" alt="Index" /></a>
201
202    </p>
203  </body>
204</html>
205
206
207
208