subprocess.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. Subprocesses
  2. ============
  3. :program:`supervisord`'s primary purpose is to create and manage
  4. processes based on data in its configuration file. It does this by
  5. creating subprocesses. Each subprocess spawned by supervisor is
  6. managed for the entirety of its lifetime by supervisord
  7. (:program:`supervisord` is the parent process of each process it
  8. creates). When a child dies, supervisor is notified of its death via
  9. the ``SIGCHLD`` signal, and it performs the appropriate operation.
  10. .. _nondaemonizing_of_subprocesses:
  11. Nondaemonizing of Subprocesses
  12. ------------------------------
  13. Programs meant to be run under supervisor should not daemonize
  14. themselves. Instead, they should run in the foreground. They should
  15. not detach from the terminal from which they are started.
  16. The easiest way to tell if a program will run in the foreground is to
  17. run the command that invokes the program from a shell prompt. If it
  18. gives you control of the terminal back, but continues running, it's
  19. daemonizing itself and that will almost certainly be the wrong way to
  20. run it under supervisor. You want to run a command that essentially
  21. requires you to press :kbd:`Ctrl-C` to get control of the terminal
  22. back. If it gives you a shell prompt back after running it without
  23. needing to press :kbd:`Ctrl-C`, it's not useful under supervisor. All
  24. programs have options to be run in the foreground but there's no
  25. "standard way" to do it; you'll need to read the documentation for
  26. each program.
  27. Below are configuration file examples that are known to start
  28. common programs in "foreground" mode under Supervisor.
  29. Examples of Program Configurations
  30. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  31. Here are some "real world" program configuration examples:
  32. Apache 2.2.6
  33. ++++++++++++
  34. .. code-block:: ini
  35. [program:apache2]
  36. command=/path/to/httpd -c "ErrorLog /dev/stdout" -DFOREGROUND
  37. redirect_stderr=true
  38. Two Zope 2.X instances and one ZEO server
  39. +++++++++++++++++++++++++++++++++++++++++
  40. .. code-block:: ini
  41. [program:zeo]
  42. command=/path/to/runzeo
  43. priority=1
  44. [program:zope1]
  45. command=/path/to/instance/home/bin/runzope
  46. priority=2
  47. redirect_stderr=true
  48. [program:zope2]
  49. command=/path/to/another/instance/home/bin/runzope
  50. priority=2
  51. redirect_stderr=true
  52. Postgres 8.X
  53. ++++++++++++
  54. .. code-block:: ini
  55. [program:postgres]
  56. command=/path/to/postmaster
  57. ; we use the "fast" shutdown signal SIGINT
  58. stopsignal=INT
  59. redirect_stderr=true
  60. OpenLDAP :program:`slapd`
  61. +++++++++++++++++++++++++
  62. .. code-block:: ini
  63. [program:slapd]
  64. command=/path/to/slapd -f /path/to/slapd.conf -h ldap://0.0.0.0:8888
  65. redirect_stderr=true
  66. Other Examples
  67. ~~~~~~~~~~~~~~
  68. Other examples of shell scripts that could be used to start services
  69. under :program:`supervisord` can be found at
  70. `http://www.thedjbway.org/services.html
  71. <http://www.thedjbway.org/services.html>`_. These examples are
  72. actually for :program:`daemontools` but the premise is the same for
  73. supervisor.
  74. Another collection of recipes for starting various programs in the
  75. foreground is available from `http://smarden.org/runit/runscripts.html
  76. <http://smarden.org/runit/runscripts.html>`_.
  77. :program:`pidproxy` Program
  78. ---------------------------
  79. Some processes (like :program:`mysqld`) ignore signals sent to the
  80. actual process which is spawned by :program:`supervisord`. Instead, a
  81. "special" thread/process is created by these kinds of programs which
  82. is responsible for handling signals. This is problematic, because
  83. :program:`supervisord` can only kill a process which it creates
  84. itself. It cannot kill not any child processes of the processes which
  85. it creates.
  86. Fortunately, these types of programs typically write a "pidfile" which
  87. contains the "special" process' PID, and is meant to be read and used
  88. in order to kill the process. As a workaround for this case, a
  89. special :program:`pidproxy` program can handle startup of these kinds
  90. of processes. The :program:`pidproxy` program is a small shim that
  91. starts a process, and upon the receipt of a signal, sends the signal
  92. to the pid provided in a pidfile. A sample configuration program
  93. entry for a pidproxy-enabled program is provided below.
  94. .. code-block:: ini
  95. [program:mysql]
  96. command=/path/to/pidproxy /path/to/pidfile /path/to/mysqld_safe
  97. The :program:`pidproxy` program is put into your configuration's
  98. ``$BINDIR`` when supervisor is installed (it is a "console script").
  99. .. _subprocess_environment:
  100. Subprocess Environment
  101. ----------------------
  102. Subprocesses will inherit the environment of the shell used to start
  103. the :program:`supervisord` program. Several environment variables
  104. will be set by :program:`supervisord` itself in the child's
  105. environment also, including :envvar:`SUPERVISOR_ENABLED` (a flag
  106. indicating the process is under supervisor control),
  107. :envvar:`SUPERVISOR_PROCESS_NAME` (the config-file-specified process
  108. name for this process) and :envvar:`SUPERVISOR_GROUP_NAME` (the
  109. config-file-specified process group name for the child process).
  110. These environment variables may be overridden within the
  111. ``[supervisord]`` section config option named ``environment`` (applies
  112. to all subprocesses) or within the per- ``[program:x]`` section
  113. ``environment`` config option (applies only to the subprocess
  114. specified within the ``[program:x]`` section). These "environment"
  115. settings are additive. In other words, each subprocess' environment
  116. will consist of:
  117. The environment variables set within the shell used to start
  118. supervisord...
  119. ... added-to/overridden-by ...
  120. ... the environment variables set within the "environment" global
  121. config option ...
  122. ... added-to/overridden-by ...
  123. ... supervisor-specific environment variables
  124. (:envvar:`SUPERVISOR_ENABLED`,
  125. :envvar:`SUPERVISOR_PROCESS_NAME`,
  126. :envvar:`SUPERVISOR_GROUP_NAME`) ..
  127. ... added-to/overridden-by ...
  128. ... the environment variables set within the per-process
  129. "environment" config option.
  130. No shell is executed by :program:`supervisord` when it runs a
  131. subprocess, so environment variables such as :envvar:`USER`,
  132. :envvar:`PATH`, :envvar:`HOME`, :envvar:`SHELL`, :envvar:`LOGNAME`,
  133. etc. are not changed from their defaults or otherwise reassigned.
  134. This is particularly important to note when you are running a program
  135. from a :program:`supervisord` run as root with a ``user=`` stanza in
  136. the configuration. Unlike :program:`cron`, :program:`supervisord`
  137. does not attempt to divine and override "fundamental" environment
  138. variables like :envvar:`USER`, :envvar:`PATH`, :envvar:`HOME`, and
  139. :envvar:`LOGNAME` when it performs a setuid to the user defined within
  140. the ``user=`` program config option. If you need to set environment
  141. variables for a particular program that might otherwise be set by a
  142. shell invocation for a particular user, you must do it explicitly
  143. within the ``environment=`` program config option. An
  144. example of setting these enviroment variables is as below.
  145. .. code-block:: ini
  146. [program:apache2]
  147. command=/home/chrism/bin/httpd -c "ErrorLog /dev/stdout" -DFOREGROUND
  148. user=chrism
  149. environment=HOME=/home/chrism,USER=chrism
  150. .. _process_states:
  151. Process States
  152. --------------
  153. A process controlled by supervisord will be in one of the below states
  154. at any given time. You may see these state names in various user
  155. interface elements in clients.
  156. ``STOPPED`` (0)
  157. The process has been stopped due to a stop request or
  158. has never been started.
  159. ``STARTING`` (10)
  160. The process is starting due to a start request.
  161. ``RUNNING`` (20)
  162. The process is running.
  163. ``BACKOFF`` (30)
  164. The process entered the ``STARTING`` state but subsequently exited
  165. too quickly to move to the ``RUNNING`` state.
  166. ``STOPPING`` (40)
  167. The process is stopping due to a stop request.
  168. ``EXITED`` (100)
  169. The process exited from the ``RUNNING`` state (expectedly or
  170. unexpectedly).
  171. ``FATAL`` (200)
  172. The process could not be started successfully.
  173. ``UNKNOWN`` (1000)
  174. The process is in an unknown state (:program:`supervisord`
  175. programming error).
  176. Each process run under supervisor progresses through these states as
  177. per the following directed graph.
  178. .. figure:: subprocess-transitions.png
  179. :alt: Subprocess State Transition Graph
  180. Subprocess State Transition Graph
  181. A process is in the ``STOPPED`` state if it has been stopped
  182. adminstratively or if it has never been started.
  183. When an autorestarting process is in the ``BACKOFF`` state, it will be
  184. automatically restarted by :program:`supervisord`. It will switch
  185. between ``STARTING`` and ``BACKOFF`` states until it becomes evident
  186. that it cannot be started because the number of ``startretries`` has
  187. exceeded the maximum, at which point it will transition to the
  188. ``FATAL`` state. Each start retry will take progressively
  189. more time.
  190. When a process is in the ``EXITED`` state, it will
  191. automatically restart:
  192. - never if its ``autorestart`` parameter is set to ``false``.
  193. - unconditionally if its ``autorestart`` parameter is set to ``true``.
  194. - conditionally if its ``autorestart`` parameter is set to
  195. ``unexpected``. If it exited with an exit code that doesn't match
  196. one of the exit codes defined in the ``exitcodes`` configuration
  197. parameter for the process, it will be restarted.
  198. A process automatically transitions from ``EXITED`` to ``RUNNING`` as
  199. a result of being configured to autorestart conditionally or
  200. unconditionally. The number of transitions between ``RUNNING`` and
  201. ``EXITED`` is not limited in any way: it is possible to create a
  202. configuration that endlessly restarts an exited process. This is a
  203. feature, not a bug.
  204. An autorestarted process will never be automtatically restarted if it
  205. ends up in the ``FATAL`` state (it must be manually restarted from
  206. this state).
  207. A process transitions into the ``STOPPING`` state via an
  208. administrative stop request, and will then end up in the
  209. ``STOPPED`` state.
  210. A process that cannot be stopped successfully will stay in the
  211. ``STOPPING`` state forever. This situation should never be reached
  212. during normal operations as it implies that the process did not
  213. respond to a final ``SIGKILL`` signal sent to it by supervisor, which
  214. is "impossible" under UNIX.
  215. State transitions which always require user action to invoke are
  216. these:
  217. ``FATAL`` -> ``STARTING``
  218. ``RUNNING`` -> ``STOPPING``
  219. State transitions which typically, but not always, require user
  220. action to invoke are these, with exceptions noted:
  221. ``STOPPED`` -> ``STARTING`` (except at supervisord startup if process
  222. is configured to autostart)
  223. ``EXITED`` -> ``STARTING`` (except if process is configured to
  224. autorestart)
  225. All other state transitions are managed by supervisord automatically.