README.txt 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. Supervisor: A System for Allowing the Control of Process State on UNIX
  2. History
  3. 7/3/2006: updated for version 2.0
  4. Introduction
  5. The supervisor is a client/server system that allows its users to
  6. control a number of processes on UNIX-like operating systems. It
  7. was inspired by the following:
  8. - It is often inconvenient to need to write "rc.d" scripts for
  9. every single process instance. rc.d scripts are a great
  10. lowest-common-denominator form of process
  11. initialization/autostart/management, but they can be painful to
  12. write and maintain. Additionally, rc.d scripts cannot
  13. automatically restart a crashed process and many programs do not
  14. restart themselves properly on a crash. Supervisord starts
  15. processes as its subprocesses, and can be configured to
  16. automatically restart them on a crash. It can also automatically
  17. be configured to start processes on its own invocation.
  18. - It's often difficult to get accurate up/down status on processes
  19. on UNIX. Pidfiles often lie. Supervisord starts processes as
  20. subprocesses, so it always knows the true up/down status of its
  21. children and can be queried conveniently for this data.
  22. - Users who need to control process state often need only to do
  23. that. They don't want or need full-blown shell access to the
  24. machine on which the processes are running. Supervisorctl allows
  25. a very limited form of access to the machine, essentially
  26. allowing users to see process status and control
  27. supervisord-controlled subprocesses by emitting "stop", "start",
  28. and "restart" commands from a simple shell or web UI.
  29. - Users often need to control processes on many machines.
  30. Supervisor provides a simple, secure, and uniform mechanism for
  31. interactively and automatically controlling processes on groups
  32. of machines.
  33. - Processes which listen on "low" TCP ports often need to be
  34. started and restarted as the root user (a UNIX misfeature). It's
  35. usually the case that it's perfectly fine to allow "normal"
  36. people to stop or restart such a process, but providing them with
  37. shell access is often impractical, and providing them with root
  38. access or sudo access is often impossible. It's also (rightly)
  39. difficult to explain to them why this problem exists. If
  40. supervisord is started as root, it is possible to allow "normal"
  41. users to control such processes without needing to explain the
  42. intricacies of the problem to them.
  43. - Processes often need to be started and stopped in groups,
  44. sometimes even in a "priority order". It's often difficult to
  45. explain to people how to do this. Supervisor allows you to
  46. assign priorities to processes, and allows user to emit commands
  47. via the supervisorctl client like "start all", and "restart all",
  48. which starts them in the preassigned priority order.
  49. Supported Platforms
  50. Supervisor has been tested and is known to run on Linux (Fedora Core
  51. 5, Ubuntu 6), Mac OS X (10.4), and Solaris (10 for Intel) and
  52. FreeBSD 6.1. It will likely work fine on most UNIX systems.
  53. Supervisor will not run at all under any version of Windows.
  54. Supervisor requires Python 2.3 or better.
  55. Installing
  56. Run "python setup.py install", then copy the "sample.conf" file to
  57. /etc/supervisord.conf and modify to your liking. If you'd rather
  58. not put the supervisord.conf file in /etc, you can place it anywhere
  59. and start supervisord and point it at the configuration file via the
  60. -c flag, e.g. "python supervisord.py -c /path/to/sample/conf" or, if
  61. you use the shell script named "supervisord", "supervisord -c
  62. /path/to/sample.conf".
  63. I make reference below to a "$BINDIR" when explaining how to run
  64. supervisord and supervisorctl. This is the "bindir" directory that
  65. your Python installation has been configured with. For example, for
  66. an installation of Python installed via "./configure
  67. --prefix=/usr/local/python; make; make install", $BINDIR would be
  68. "/usr/local/python/bin". Python interpreters on different platforms
  69. use different $BINDIRs. Look at the output of "setup.py install" if
  70. you can't figure out where yours is.
  71. Running Supervisord
  72. To start supervisord, run $BINDIR/supervisord. The resulting
  73. process will daemonize itself and detach from the terminal. It
  74. keeps an operations log at "/tmp/supervisor.log" by default.
  75. You can start supervisord in the foreground by passing the "-n" flag
  76. on its command line. This is useful to debug startup problems.
  77. To change the set of programs controlled by supervisord, edit the
  78. supervisord.conf file and kill -HUP or otherwise restart the
  79. supervisord process. This file has several example program
  80. definitions.
  81. Supervisord accepts a number of command-line overrides. Type
  82. 'supervisord -h' for an overview.
  83. Running Supervisorctl
  84. To start supervisorctl, run $BINDIR/supervisorctl. A shell will
  85. be presented that will allow you to control the processes that are
  86. currently managed by supervisord. Type "help" at the prompt to get
  87. information about the supported commands.
  88. supervisorctl may be invoked with "one time" commands when invoked
  89. with arguments from a command line. An example: "supervisorctl stop
  90. all". If arguments are present on the supervisorctl command-line,
  91. it will prevent the interactive shell from being invoked. Instead,
  92. the command will be executed and supervisorctl will exit.
  93. If supervisorctl is invoked in interactive mode against a
  94. supervisord that requires authentication, you will be asked for
  95. authentication credentials.
  96. Components
  97. Supervisord
  98. The server piece of the supervisor is named "supervisord". It is
  99. responsible for responding to commands from the client process as
  100. well as restarting crashed processes. It is meant to be run as
  101. the root user in most production setups. NOTE: see "Security
  102. Notes" at the end of this document for caveats!
  103. The server process uses a configuration file. This is typically
  104. located in "/etc/supervisord.conf". This configuration file is an
  105. "Windows-INI" style config file. It is important to keep this
  106. file secure via proper filesystem permissions because it may
  107. contain unencrypted usernames and passwords.
  108. Supervisorctl
  109. The command-line client piece of the supervisor is named
  110. "supervisorctl". It provides a shell-like interface to the
  111. features provided by supervisord. From supervisorctl, a user can
  112. connect to different supervisord processes, get status on the
  113. subprocesses controlled by a supervisord, stop and start
  114. subprocesses of a supervisord, and get lists of running processes
  115. of a supervisord.
  116. The command-line client talks to the server across a UNIX domain
  117. socket or an Internet socket. The server can assert that the user
  118. of a client should present authentication credentials before it
  119. allows him to perform commands. The client process may use the
  120. same configuration file as the server; any configuration file with
  121. a [supervisorctl] section in it will work.
  122. Web Server
  123. A (sparse) web user interface with functionality comparable to
  124. supervisorctl may be accessed via a browser if you start
  125. supervisord against an internet socket. Visit the server URL
  126. (e.g. http://localhost:9001/) to view and control process status
  127. through the web interface after changing the configuration file's
  128. 'http_port' parameter appropriately.
  129. XML-RPC Interface
  130. The same HTTP server which serves the web UI serves up an XML-RPC
  131. interface that can be used to interrogate and control supervisor
  132. and the programs it runs. To use the XML-RPC interface, connect
  133. to supervisor's http port with any XML-RPC client library and run
  134. commands against it. An example of doing this using Python's
  135. xmlrpclib client library::
  136. import xmlrpclib
  137. server = xmlrpclib.Server('http://localhost:9001')
  138. Call methods against the supervisor and its subprocesses by using
  139. the 'supervisor' namespace::
  140. server.supervisor.getState()
  141. You can get a list of methods supported by supervisor's XML-RPC
  142. interface by using the XML-RPC 'system.listMethods' API:
  143. server.system.listMethods()
  144. You can see help on a method by using the 'system.methodHelp' API
  145. against the method::
  146. print server.system.methodHelp('supervisor.shutdown')
  147. Supervisor's XML-RPC interface also supports the nascent XML-RPC
  148. multicall API described at
  149. http://www.xmlrpc.com/discuss/msgReader$1208.
  150. Configuration File '[supervisord]' Section Settings
  151. The supervisord.conf log file contains a section named
  152. '[supervisord]' in which global settings for the supervisord process
  153. should be inserted. These are:
  154. 'http_port' -- Either a TCP host:port value or (e.g. 127.0.0.1:9001)
  155. or a path to a UNIX domain socket (e.g. /tmp/supervisord.sock) on
  156. which supervisor will listen for HTTP/XML-RPC requests.
  157. Supervisorctl itself uses XML-RPC to communicate with supervisord
  158. over this port.
  159. 'sockchmod' -- Change the UNIX permission mode bits of the http_port
  160. UNIX domain socket to this value (ignored if using a TCP socket).
  161. Default: 0700.
  162. 'sockchown' -- Change the user and group of the socket file to this
  163. value. May be a username (e.g. chrism) or a username and group
  164. separated by a dot (e.g. chrism.wheel) Default: do not change.
  165. 'umask' -- The umask of the supervisord process. Default: 022.
  166. 'logfile' -- The path to the activity log of the supervisord process.
  167. 'logfile_maxbytes' -- The maximum number of bytes that may be
  168. consumed by the activity log file before it is rotated (suffix
  169. multipliers like "KB", "MB", and "GB" can be used in the value).
  170. Set this value to 0 to indicate an unlimited log size. Default:
  171. 50MB.
  172. 'logfile_backups' -- The number of backups to keep around resulting
  173. from activity log file rotation. Set this to 0 to indicate an
  174. unlimited number of backups. Default: 10.
  175. 'loglevel' -- The logging level, dictating what is written to the
  176. activity log. One of 'critical', 'error', 'warn', 'info', 'debug'
  177. or 'trace'. At log level 'trace', the supervisord log file will
  178. record the stderr/stdout output of its child processes, which is
  179. useful for debugging. Default: info.
  180. 'pidfile' -- The location in which supervisord keeps its pid file.
  181. 'nodaemon' -- If true, supervisord will start in the foreground
  182. instead of daemonizing. Default: false.
  183. 'minfds' -- The minimum number of file descriptors that must be
  184. available before supervisord will start successfully. Default:
  185. 1024.
  186. 'minprocs' -- The minimum nymber of process descriptors that must be
  187. available before supervisord will start successfully. Default: 200.
  188. 'nocleanup' -- prevent supervisord from clearing old "AUTO" log
  189. files at startup time. Default: false.
  190. 'http_username' -- the username required for authentication to our
  191. HTTP server. Default: none.
  192. 'http_password' -- the password required for authentication to our
  193. HTTP server. Default: none.
  194. 'childlogdir' -- the directory used for AUTO log files. Default:
  195. value of Python's tempfile.get_tempdir().
  196. 'user' -- if supervisord is run as root, switch users to this UNIX
  197. user account before doing any meaningful processing. This value has
  198. no effect if supervisord is not run as root. Default: do not switch
  199. users.
  200. 'directory' -- When supervisord daemonizes, switch to this
  201. directory. Default: do not cd.
  202. Configuration File '[supervisorctl]' Section Settings
  203. The configuration file may contain settings for the supervisorctl
  204. interactive shell program. These options are listed below.
  205. 'serverurl' -- The URL that should be used to access the supervisord
  206. server, e.g. "http://localhost:9001". For UNIX domain sockets, use
  207. "unix:///absolute/path/to/file.sock".
  208. 'username' -- The username to pass to the supervisord server for use
  209. in authentication (should be same as 'http_username' in supervisord
  210. config). Optional.
  211. 'password' -- The password to pass to the supervisord server for use
  212. in authentication (should be the same as 'http_password' in
  213. supervisord config). Optional.
  214. 'prompt' -- String used as supervisorctl prompt. Default: supervisor.
  215. Configuration File '[program:x]' Section Settings
  216. The .INI file must contain one or more 'program' sections in order
  217. for supervisord to know which programs it should start and control.
  218. A sample program section has the following structure, the options of
  219. which are described below it::
  220. [program:programname]
  221. command=/path/to/programname
  222. priority=1
  223. autostart=true
  224. autorestart=true
  225. startsecs=10
  226. startretries=3
  227. exitcodes=0,2
  228. stopsignal=TERM
  229. stopwaitsecs=10
  230. user=nobody
  231. log_stdout=true
  232. log_stderr=false
  233. logfile=/tmp/programname.log
  234. logfile_maxbytes=10MB
  235. logfile_backups=2
  236. '[program:programname]' -- the section header, required for each
  237. program. 'programname' is a descriptive name (arbitrary) used to
  238. describe the program being run.
  239. 'command' -- the command that will be run when this program is
  240. started. The command can be either absolute,
  241. e.g. ('/path/to/programname') or relative ('programname'). If it is
  242. relative, the PATH will be searched for the executable. Programs
  243. can accept arguments, e.g. ('/path/to/program foo bar'). The
  244. command line can used double quotes to group arguments with spaces
  245. in them to pass to the program, e.g. ('/path/to/program/name -p "foo
  246. bar"'). Controlled programs should themselves not be daemons, as
  247. supervisord assumes it is responsible for daemonizing its
  248. subprocesses.
  249. 'priority' -- the relative priority of the program in the start and
  250. shutdown ordering. Lower priorities indicate programs that start
  251. first and shut down last at startup and when aggregate commands are
  252. used in various clients (e.g. "start all"/"stop all"). Higher
  253. priorities indicate programs that start last and shut down first.
  254. Default: 999.
  255. 'autostart' -- If true, this program will start automatically when
  256. supervisord is started. Default: true.
  257. 'autorestart' -- If true, when the program exits "unexpectedly",
  258. supervisor will restart it automatically. "unexpected" exits are
  259. those which happen when the program exits with an "unexpected" exit
  260. code (see 'exitcodes'). Default: true.
  261. 'startsecs' -- The total number of seconds which the program needs
  262. to stay running after a startup to consider the start successful.
  263. If the program does not stay up for this many seconds after it is
  264. started, even if it exits with an "expected" exit code, the startup
  265. will be considered a failure. Set to 0 to indicate that the program
  266. needn't stay running for any particular amount of time. Default: 1.
  267. 'startretries' -- The number of serial failure attempts that
  268. supervisord will allow when attempting to start the program before
  269. giving up and puting the process into an ERROR state. Default: 3.
  270. 'exitcodes' -- The list of 'expected' exit codes for this program.
  271. A program is considered 'failed' (and will be restarted, if
  272. autorestart is set true) if it exits with an exit code which is not
  273. in this list and a stop of the program has not been explicitly
  274. requested. Default: 0,2.
  275. 'stopsignal' -- The signal used to kill the program when a stop is
  276. requested. This can be any of TERM, HUP, INT, QUIT, KILL, USR1, or
  277. USR2. Default: TERM.
  278. 'stopwaitsecs' -- The number of seconds to wait for the program to
  279. return a SIGCHILD to supervisord after the program has been sent a
  280. stopsignal. If this number of seconds elapses before supervisord
  281. receives a SIGCHILD from the process, supervisord will attempt to
  282. kill it with a final SIGKILL. Default: 10.
  283. 'user' -- If supervisord is running as root, this UNIX user account
  284. will be used as the account which runs the program. If supervisord
  285. is not running as root, this option has no effect. Defaut: do not
  286. switch users.
  287. 'log_stdout' -- Send process stdout output to the process logfile.
  288. Default: true.
  289. 'log_stderr' -- Send process stderr output to the process logfile.
  290. Default: false.
  291. 'logfile' -- Keep process output as determined by log_stdout and
  292. log_stderr in this file. NOTE: if both log_stderr and log_stdout
  293. are true, chunks of output from the process' stderr and stdout will
  294. be intermingled more or less randomly in the log. If 'logfile' is
  295. unset or set to 'AUTO', supervisor will automatically choose a file
  296. location. If this is set to 'NONE', supervisord will create no log
  297. file. AUTO log files and their backups will be deleted when
  298. supervisord restarts. Default: AUTO.
  299. 'logfile_maxbytes' -- The maximum number of bytes that may be
  300. consumed by the process log file before it is rotated (suffix
  301. multipliers like "KB", "MB", and "GB" can be used in the value).
  302. Set this value to 0 to indicate an unlimited log size. Default:
  303. 50MB.
  304. 'logfile_backups' -- The number of backups to keep around resulting
  305. from process log file rotation. Set this to 0 to indicate an
  306. unlimited number of backups. Default: 10.
  307. Nondaemonizing of Subprocesses
  308. Programs run under supervisor *should not* daemonize themselves.
  309. Instead, they should run in the foreground and not detach from the
  310. "terminal" that starts them. The easiest way to tell if a command
  311. will run in the foreground is to run the command from a shell
  312. prompt. If it gives you control of the terminal back, it's
  313. daemonizing itself and that will be the wrong way to run it under
  314. supervisor. You want to run a command that essentially requires you
  315. to press Ctrl-C to get control of the terminal back. If it gives
  316. you a shell prompt back after running it without needing to press
  317. Ctrl-C, it's not useful under supervisor. All programs have options
  318. to be run in the foreground but there's no standard way to do it;
  319. you'll need to read the documentation for each program you want to
  320. do this with.
  321. Examples of Program Configurations
  322. Apache 2.0.54::
  323. [program:apache]
  324. command=/usr/sbin/httpd -DNO_DETACH
  325. Postgres 8.14::
  326. [program:postgres]
  327. command=/path/to/postmaster
  328. ; we use the "fast" shutdown signal SIGINT
  329. stopsignal=INT
  330. log_stderr=true
  331. Zope 2.8 instances and ZEO::
  332. [program:zeo]
  333. command=/path/to/runzeo
  334. priority=1
  335. [program:zope1]
  336. command=/path/to/instance/home/bin/runzope
  337. priority=2
  338. log_stderr=true
  339. [program:zope2]
  340. command=/path/to/another/instance/home/bin/runzope
  341. priority=2
  342. log_stderr=true
  343. OpenLDAP slapd::
  344. [program:slapd]
  345. command=/path/to/slapd -f /path/to/slapd.conf -h ldap://0.0.0.0:8888
  346. Process States
  347. A process controlled by supervisord will be in one of the below
  348. states at any given time. You may see these state names in various
  349. user interface elements.
  350. STOPPED (0) -- The process has been stopped due to a stop request or
  351. has never been started.
  352. STARTING (10) -- The process is starting due to a start request.
  353. RUNNING (20) -- The process is running.
  354. BACKOFF (30) -- The process is waiting to restart after a nonfatal error.
  355. STOPPING (40) -- The process is stopping due to a stop request.
  356. EXITED (100) -- The process exited with an expected exit code.
  357. FATAL (200) -- The process could not be started successfully.
  358. UNKNOWN (1000) -- The process is in an unknown state (programming error).
  359. Process progress through these states as per the following directed
  360. graph::
  361. STOPPED
  362. ^ |
  363. / |
  364. STOPPING |
  365. ^ V
  366. | STARTING <-----> BACKOFF
  367. | / \
  368. | V V
  369. \-- RUNNING FATAL
  370. |
  371. V
  372. EXITED
  373. A process is in the STOPPED state if it has been stopped
  374. adminstratively or if it has never been started.
  375. When an autorestarting process is in the BACKOFF state, it will be
  376. automatically restarted by supervisord. It will switch between
  377. STARTING and BACKOFF states until it becomes evident that it cannot
  378. be started because the number of startretries has exceeded the
  379. maximum, at which point it will transition to the FATAL state. Each
  380. start retry will take progressively more time.
  381. An autorestarted process will never be automtatically restarted if
  382. it ends up in the FATAL state (it must be manually restarted from
  383. this state).
  384. A process transitions into the STOPPING state via an administrative
  385. stop request, and will then end up in the STOPPED state.
  386. A process that cannot be stopped successfully will stay in the
  387. STOPPING state forever. This situation should never be reached
  388. during normal operations as it implies that the process did not
  389. respond to a final SIGKILL, which is "impossible" under UNIX.
  390. Terminal states are "STOPPED", "FATAL", "EXITED", and "UNKNOWN".
  391. All other states are transitional.
  392. Signals
  393. Killing supervisord with SIGHUP will stop all processes, reload the
  394. configuration from the config file, and restart all processes.
  395. Killing supervisord with SIGUSR2 will close and reopen the
  396. supervisord activity log and child log files.
  397. Access Control
  398. The UNIX permissions on the socket effectively control who may send
  399. commands to the server. HTTP basic authentication provides access
  400. control for internet and UNIX domain sockets as necessary.
  401. Security Notes
  402. I have done my best to assure that use of a supervisord process
  403. running as root cannot lead to unintended privilege escalation, but
  404. caveat emptor. Particularly, it is not as paranoid as something
  405. like DJ Bernstein's "daemontools", inasmuch as "supervisord" allows
  406. for arbitrary path specifications in its configuration file to which
  407. data may be written. Allowing arbitrary path selections can create
  408. vulnerabilities from symlink attacks. Be careful when specifying
  409. paths in your configuration. Ensure that supervisord's
  410. configuration file cannot be read from or written to by unprivileged
  411. users and that all files installed by the supervisor package have
  412. "sane" file permission protection settings. Additionally, ensure
  413. that your PYTHONPATH is sane and that all Python standard library
  414. files have adequate file permission protections. Then, pray to the
  415. deity of your choice.
  416. Other Notes
  417. Some examples of shell scripts to start services under supervisor
  418. can be found "here":http://www.thedjbway.org/services.html. These
  419. examples are actually for daemontools but the premise is the same
  420. for supervisor. Another collection of recipes for starting various
  421. programs in the foreground is
  422. "here":http://smarden.org/runit/runscripts.html .
  423. Some processes (like mysqld) ignore signals sent to the actual
  424. process/thread which is created by supervisord. Instead, a
  425. "special" thread/process is created by these kinds of programs which
  426. is responsible for handling signals. This is problematic, because
  427. supervisord can only kill a pid which it creates itself, not any
  428. child thread or process of the program it creates. Fortunately,
  429. these programs typically write a pidfile which is meant to be read
  430. in order to kill the process. As a workaround for this case, a
  431. special "pidproxy" program can handle startup of these kinds of
  432. processes. The pidproxy program is a small shim that starts a
  433. process, and upon the receipt of a signal, sends the signal to the
  434. pid provided in a pidfile. A sample supervisord configuration
  435. program entry for a pidproxy-enabled program is provided here::
  436. [program:mysql]
  437. command=/path/to/pidproxy /path/to/pidfile /path/to/mysqld_safe
  438. The pidproxy program is named 'pidproxy.py' and is in the
  439. distribution.
  440. FAQ
  441. My program never starts and supervisor doesn't indicate any error:
  442. Make sure the "x" bit is set on the executable file you're using in
  443. the command= line.
  444. How can I tell if my program is running under supervisor? Supervisor
  445. and its subprocesses share an environment variable
  446. "SUPERVISOR_ENABLED". When a process is run under supervisor, your
  447. program can check for the presence of this variable to determine
  448. whether it is running under supervisor (new in 2.0).
  449. Reporting Bugs
  450. Please report bugs at http://www.plope.com/software/collector .
  451. Author Information
  452. Chris McDonough (chrism@plope.com)
  453. http://www.plope.com