subprocess.rst 10 KB

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