preserve user/group ownership when reopening logs
[unicorn.git] / SIGNALS
blob5c7295e64a76710ae29fc379ea9537544b61def7
1 == Signal handling
3 In general, signals need only be sent to the master process.  However,
4 the signals Unicorn uses internally to communicate with the worker
5 processes are documented here as well.  With the exception of TTIN/TTOU,
6 signal handling matches the behavior of {nginx}[http://nginx.net/] so it
7 should be possible to easily share process management scripts between
8 Unicorn and nginx.
10 === Master Process
12 * HUP - reload config file, app, and gracefully restart all workers
14 * INT/TERM - quick shutdown, kills all workers immediately
16 * QUIT - graceful shutdown, waits for workers to finish their
17   current request before finishing.
19 * USR1 - reopen all logs owned by the master and all workers
20   See Unicorn::Util.reopen_logs for what is considered a log.
22 * USR2 - reexecute the running binary.  A separate QUIT
23   should be sent to the original process once the child is verified to
24   be up and running.
26 * WINCH - gracefully stops workers but keep the master running.
27   This will only work for daemonized processes.
29 * TTIN - increment the number of worker processes by one
31 * TTOU - decrement the number of worker processes by one
33 === Worker Processes
35 Sending signals directly to the worker processes should not normally be
36 needed.  If the master process is running, any exited worker will be
37 automatically respawned.
39 * INT/TERM - Quick shutdown, immediately exit.
40   Unless WINCH has been sent to the master (or the master is killed),
41   the master process will respawn a worker to replace this one.
43 * QUIT - Gracefully exit after finishing the current request.
44   Unless WINCH has been sent to the master (or the master is killed),
45   the master process will respawn a worker to replace this one.
47 * USR1 - Reopen all logs owned by the worker process.
48   See Unicorn::Util.reopen_logs for what is considered a log.
49   Log files are not reopened until it is done processing
50   the current request, so multiple log lines for one request
51   (as done by Rails) will not be split across multiple logs.
53   It is NOT recommended to send the USR1 signal directly to workers via
54   "killall -USR1 unicorn" if you are using user/group-switching support
55   in your workers.  You will encounter incorrect file permissions and
56   workers will need to be respawned.  Sending USR1 to the master process
57   first will ensure logs have the correct permissions before the master
58   forwards the USR1 signal to workers.
60 === Procedure to replace a running unicorn executable
62 You may replace a running instance of unicorn with a new one without
63 losing any incoming connections.  Doing so will reload all of your
64 application code, Unicorn config, Ruby executable, and all libraries.
65 The only things that will not change (due to OS limitations) are:
67 1. The path to the unicorn executable script.  If you want to change to
68    a different installation of Ruby, you can modify the shebang
69    line to point to your alternative interpreter.
71 The procedure is exactly like that of nginx:
73 1. Send USR2 to the master process
75 2. Check your process manager or pid files to see if a new master spawned
76    successfully.  If you're using a pid file, the old process will have
77    ".oldbin" appended to its path.  You should have two master instances
78    of unicorn running now, both of which will have workers servicing
79    requests.  Your process tree should look something like this:
81      unicorn master (old)
82      \_ unicorn worker[0]
83      \_ unicorn worker[1]
84      \_ unicorn worker[2]
85      \_ unicorn worker[3]
86      \_ unicorn master
87         \_ unicorn worker[0]
88         \_ unicorn worker[1]
89         \_ unicorn worker[2]
90         \_ unicorn worker[3]
92 3. You can now send WINCH to the old master process so only the new workers
93    serve requests.  If your unicorn process is bound to an interactive
94    terminal, you can skip this step.  Step 5 will be more difficult but
95    you can also skip it if your process is not daemonized.
97 4. You should now ensure that everything is running correctly with the
98    new workers as the old workers die off.
100 5. If everything seems ok, then send QUIT to the old master.  You're done!
102    If something is broken, then send HUP to the old master to reload
103    the config and restart its workers.  Then send QUIT to the new master
104    process.