1. Supervisor简介

Supervisord 是用 Python 实现的一款的进程管理工具,supervisord 要求管理的程序是非 daemon 程序,supervisord 会帮你把它转成 daemon 程序,因此如果用 supervisord 来管理进程,进程需要以非daemon的方式启动。

例如:管理nginx 的话,必须在 nginx 的配置文件里添加一行设置 daemon off 让 nginx 以非 daemon 方式启动。

2. Supervisor安装

以centos系统为例,以下两种方式选择其一。

  1. # yum install 的方式
  2. yum install -y supervisor
  3. # easy_install的方式
  4. yum install -y python-setuptools
  5. easy_install supervisor
  6. echo_supervisord_conf >/etc/supervisord.conf

3. Supervisor的配置

3.1. supervisord.conf的配置

如果使用yum install -y supervisor的命令安装,会生成默认配置/etc/supervisord.conf和目录/etc/supervisord.d,如果没有则自行创建。

/etc/supervisord.d的目录下创建conflog两个目录,conf用于存放管理进程的配置,log用于存放管理进程的日志。

  1. cd /etc/supervisord.d
  2. mkdir conf log

修改/etc/supervisord.conf[include]部分,即载入/etc/supervisord.d/conf目录下的所有配置。

  1. vi /etc/supervisord.conf
  2. ...
  3. [include]
  4. files = supervisord.d/conf/*.conf
  5. ...

也可以修改supervisor应用日志的目录,默认日志路径为/var/log/supervisor/supervisord.log

  1. vi /etc/supervisord.conf
  2. ...
  3. [supervisord]
  4. logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
  5. logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
  6. logfile_backups=10 ; (num of main logfile rotation backups;default 10)
  7. loglevel=info ; (log level;default info; others: debug,warn,trace)
  8. pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
  9. ...

3.2. 管理应用的配置

进入到/etc/supervisord.d/conf目录,创建管理应用的配置,可以创建多个应用配置。

例如,创建confd.conf配置。

  1. [program:confd]
  2. directory = /usr/local/bin ; 程序的启动目录
  3. command = /usr/local/bin/confd -config-file /etc/confd/confd.toml ; 启动命令,与命令行启动的命令是一样的
  4. autostart = true ; supervisord 启动的时候也自动启动
  5. startsecs = 5 ; 启动 5 秒后没有异常退出,就当作已经正常启动了
  6. autorestart = true ; 程序异常退出后自动重启
  7. startretries = 3 ; 启动失败自动重试次数,默认是 3
  8. user = root ; 用哪个用户启动
  9. redirect_stderr = true ; stderr 重定向到 stdout,默认 false
  10. stdout_logfile_maxbytes = 20MB ; stdout 日志文件大小,默认 50MB
  11. stdout_logfile_backups = 20 ; stdout 日志文件备份数
  12. ; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)
  13. stdout_logfile = /etc/supervisord.d/log/confd.log ;日志统一放在log目录下
  14. ; 可以通过 environment 来添加需要的环境变量,一种常见的用法是修改 PYTHONPATH
  15. ; environment=PYTHONPATH=$PYTHONPATH:/path/to/somewhere

4. Surpervisor的启动

  1. # supervisord二进制启动
  2. supervisord -c /etc/supervisord.conf
  3. # 检查进程
  4. ps aux | grep supervisord

或者以systemd的方式管理

vi /etc/rc.d/init.d/supervisord

  1. #!/bin/sh
  2. #
  3. # /etc/rc.d/init.d/supervisord
  4. #
  5. # Supervisor is a client/server system that
  6. # allows its users to monitor and control a
  7. # number of processes on UNIX-like operating
  8. # systems.
  9. #
  10. # chkconfig: - 64 36
  11. # description: Supervisor Server
  12. # processname: supervisord
  13. # Source init functions
  14. . /etc/rc.d/init.d/functions
  15. prog="supervisord"
  16. prefix="/usr"
  17. exec_prefix="${prefix}"
  18. prog_bin="${exec_prefix}/bin/supervisord"
  19. PIDFILE="/var/run/$prog.pid"
  20. start()
  21. {
  22. echo -n $"Starting $prog: "
  23. daemon $prog_bin --pidfile $PIDFILE -c /etc/supervisord.conf
  24. [ -f $PIDFILE ] && success $"$prog startup" || failure $"$prog startup"
  25. echo
  26. }
  27. stop()
  28. {
  29. echo -n $"Shutting down $prog: "
  30. [ -f $PIDFILE ] && killproc $prog || success $"$prog shutdown"
  31. echo
  32. }
  33. case "$1" in
  34. start)
  35. start
  36. ;;
  37. stop)
  38. stop
  39. ;;
  40. status)
  41. status $prog
  42. ;;
  43. restart)
  44. stop
  45. start
  46. ;;
  47. *)
  48. echo "Usage: $0 {start|stop|restart|status}"
  49. ;;
  50. esac

设置开机启动及systemd方式启动。

  1. sudo chmod +x /etc/rc.d/init.d/supervisord
  2. sudo chkconfig --add supervisord
  3. sudo chkconfig supervisord on
  4. sudo service supervisord start

5. supervisorctl&supervisord

Supervisord 安装完成后有两个可用的命令行 supervisordsupervisorctl,命令使用解释如下:

5.1. supervisorctl

  • supervisorctl stop programxxx,停止某一个进程(programxxx),programxxx 为 [program:beepkg] 里配置的值,这个示例就是 beepkg。
  • supervisorctl start programxxx,启动某个进程。
  • supervisorctl restart programxxx,重启某个进程。
  • supervisorctl status,查看进程状态。
  • supervisorctl stop groupworker ,重启所有属于名为 groupworker 这个分组的进程(start,restart 同理)。
  • supervisorctl stop all,停止全部进程,注:start、restart、stop 都不会载入最新的配置文件。
  • supervisorctl reload,载入最新的配置文件,停止原有进程并按新的配置启动、管理所有进程。
  • supervisorctl update,根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启。

更多参考:

  1. $ supervisorctl --help
  2. supervisorctl -- control applications run by supervisord from the cmd line.
  3. Usage: /usr/bin/supervisorctl [options] [action [arguments]]
  4. Options:
  5. -c/--configuration -- configuration file path (default /etc/supervisord.conf)
  6. -h/--help -- print usage message and exit
  7. -i/--interactive -- start an interactive shell after executing commands
  8. -s/--serverurl URL -- URL on which supervisord server is listening
  9. (default "http://localhost:9001").
  10. -u/--username -- username to use for authentication with server
  11. -p/--password -- password to use for authentication with server
  12. -r/--history-file -- keep a readline history (if readline is available)
  13. action [arguments] -- see below
  14. Actions are commands like "tail" or "stop". If -i is specified or no action is
  15. specified on the command line, a "shell" interpreting actions typed
  16. interactively is started. Use the action "help" to find out about available
  17. actions.

例如:

  1. # supervisorctl status
  2. confd RUNNING pid 31256, uptime 0:11:24
  3. twemproxy RUNNING pid 31255, uptime 0:11:24

5.2. supervisord

  • supervisord,初始启动 Supervisord,启动、管理配置中设置的进程。
  1. $ supervisord --help
  2. supervisord -- run a set of applications as daemons.
  3. Usage: /usr/bin/supervisord [options]
  4. Options:
  5. -c/--configuration FILENAME -- configuration file
  6. -n/--nodaemon -- run in the foreground (same as 'nodaemon true' in config file)
  7. -h/--help -- print this usage message and exit
  8. -v/--version -- print supervisord version number and exit
  9. -u/--user USER -- run supervisord as this user (or numeric uid)
  10. -m/--umask UMASK -- use this umask for daemon subprocess (default is 022)
  11. -d/--directory DIRECTORY -- directory to chdir to when daemonized
  12. -l/--logfile FILENAME -- use FILENAME as logfile path
  13. -y/--logfile_maxbytes BYTES -- use BYTES to limit the max size of logfile
  14. -z/--logfile_backups NUM -- number of backups to keep when max bytes reached
  15. -e/--loglevel LEVEL -- use LEVEL as log level (debug,info,warn,error,critical)
  16. -j/--pidfile FILENAME -- write a pid file for the daemon process to FILENAME
  17. -i/--identifier STR -- identifier used for this instance of supervisord
  18. -q/--childlogdir DIRECTORY -- the log directory for child process logs
  19. -k/--nocleanup -- prevent the process from performing cleanup (removal of
  20. old automatic child log files) at startup.
  21. -a/--minfds NUM -- the minimum number of file descriptors for start success
  22. -t/--strip_ansi -- strip ansi escape codes from process output
  23. --minprocs NUM -- the minimum number of processes available for start success
  24. --profile_options OPTIONS -- run supervisord under profiler and output
  25. results based on OPTIONS, which is a comma-sep'd
  26. list of 'cumulative', 'calls', and/or 'callers',
  27. e.g. 'cumulative,callers')

6. Supervisor控制台

/etc/supervisord.conf中修改[inet_http_server]的参数,具体如下:

  1. [inet_http_server] ; inet (TCP) server disabled by default
  2. port=*:9001 ; ip_address:port specifier, *:port for all iface
  3. username=root ; default is no username (open server)
  4. password=xxxx ; default is no password (open server)

修改后重启supervisor进程,在浏览器访问 http://<host-ip>:9001

具体如下:

supervisor

7. supervisor.conf详细配置

cat /etc/supervisord.conf

  1. ; Sample supervisor config file.
  2. [unix_http_server]
  3. file=/var/run/supervisor/supervisor.sock ; (the path to the socket file)
  4. ;chmod=0700 ; sockef file mode (default 0700)
  5. ;chown=nobody:nogroup ; socket file uid:gid owner
  6. ;username=user ; (default is no username (open server))
  7. ;password=123 ; (default is no password (open server))
  8. ;[inet_http_server] ; inet (TCP) server disabled by default
  9. ;port=127.0.0.1:9001 ; (ip_address:port specifier, *:port for all iface)
  10. ;username=user ; (default is no username (open server))
  11. ;password=123 ; (default is no password (open server))
  12. [supervisord]
  13. logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
  14. logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
  15. logfile_backups=10 ; (num of main logfile rotation backups;default 10)
  16. loglevel=info ; (log level;default info; others: debug,warn,trace)
  17. pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
  18. nodaemon=false ; (start in foreground if true;default false)
  19. minfds=1024 ; (min. avail startup file descriptors;default 1024)
  20. minprocs=200 ; (min. avail process descriptors;default 200)
  21. ;umask=022 ; (process file creation umask;default 022)
  22. ;user=chrism ; (default is current user, required if root)
  23. ;identifier=supervisor ; (supervisord identifier, default is 'supervisor')
  24. ;directory=/tmp ; (default is not to cd during start)
  25. ;nocleanup=true ; (don't clean up tempfiles at start;default false)
  26. ;childlogdir=/tmp ; ('AUTO' child log dir, default $TEMP)
  27. ;environment=KEY=value ; (key value pairs to add to environment)
  28. ;strip_ansi=false ; (strip ansi escape codes in logs; def. false)
  29. ; the below section must remain in the config file for RPC
  30. ; (supervisorctl/web interface) to work, additional interfaces may be
  31. ; added by defining them in separate rpcinterface: sections
  32. [rpcinterface:supervisor]
  33. supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
  34. [supervisorctl]
  35. serverurl=unix:///var/run/supervisor/supervisor.sock ; use a unix:// URL for a unix socket
  36. ;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
  37. ;username=chris ; should be same as http_username if set
  38. ;password=123 ; should be same as http_password if set
  39. ;prompt=mysupervisor ; cmd line prompt (default "supervisor")
  40. ;history_file=~/.sc_history ; use readline history if available
  41. ; The below sample program section shows all possible program subsection values,
  42. ; create one or more 'real' program: sections to be able to control them under
  43. ; supervisor.
  44. ;[program:theprogramname]
  45. ;command=/bin/cat ; the program (relative uses PATH, can take args)
  46. ;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
  47. ;numprocs=1 ; number of processes copies to start (def 1)
  48. ;directory=/tmp ; directory to cwd to before exec (def no cwd)
  49. ;umask=022 ; umask for process (default None)
  50. ;priority=999 ; the relative start priority (default 999)
  51. ;autostart=true ; start at supervisord start (default: true)
  52. ;autorestart=true ; retstart at unexpected quit (default: true)
  53. ;startsecs=10 ; number of secs prog must stay running (def. 1)
  54. ;startretries=3 ; max # of serial start failures (default 3)
  55. ;exitcodes=0,2 ; 'expected' exit codes for process (default 0,2)
  56. ;stopsignal=QUIT ; signal used to kill process (default TERM)
  57. ;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
  58. ;user=chrism ; setuid to this UNIX account to run the program
  59. ;redirect_stderr=true ; redirect proc stderr to stdout (default false)
  60. ;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO
  61. ;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
  62. ;stdout_logfile_backups=10 ; # of stdout logfile backups (default 10)
  63. ;stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
  64. ;stdout_events_enabled=false ; emit events on stdout writes (default false)
  65. ;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO
  66. ;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
  67. ;stderr_logfile_backups=10 ; # of stderr logfile backups (default 10)
  68. ;stderr_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
  69. ;stderr_events_enabled=false ; emit events on stderr writes (default false)
  70. ;environment=A=1,B=2 ; process environment additions (def no adds)
  71. ;serverurl=AUTO ; override serverurl computation (childutils)
  72. ; The below sample eventlistener section shows all possible
  73. ; eventlistener subsection values, create one or more 'real'
  74. ; eventlistener: sections to be able to handle event notifications
  75. ; sent by supervisor.
  76. ;[eventlistener:theeventlistenername]
  77. ;command=/bin/eventlistener ; the program (relative uses PATH, can take args)
  78. ;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
  79. ;numprocs=1 ; number of processes copies to start (def 1)
  80. ;events=EVENT ; event notif. types to subscribe to (req'd)
  81. ;buffer_size=10 ; event buffer queue size (default 10)
  82. ;directory=/tmp ; directory to cwd to before exec (def no cwd)
  83. ;umask=022 ; umask for process (default None)
  84. ;priority=-1 ; the relative start priority (default -1)
  85. ;autostart=true ; start at supervisord start (default: true)
  86. ;autorestart=unexpected ; restart at unexpected quit (default: unexpected)
  87. ;startsecs=10 ; number of secs prog must stay running (def. 1)
  88. ;startretries=3 ; max # of serial start failures (default 3)
  89. ;exitcodes=0,2 ; 'expected' exit codes for process (default 0,2)
  90. ;stopsignal=QUIT ; signal used to kill process (default TERM)
  91. ;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
  92. ;user=chrism ; setuid to this UNIX account to run the program
  93. ;redirect_stderr=true ; redirect proc stderr to stdout (default false)
  94. ;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO
  95. ;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
  96. ;stdout_logfile_backups=10 ; # of stdout logfile backups (default 10)
  97. ;stdout_events_enabled=false ; emit events on stdout writes (default false)
  98. ;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO
  99. ;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
  100. ;stderr_logfile_backups ; # of stderr logfile backups (default 10)
  101. ;stderr_events_enabled=false ; emit events on stderr writes (default false)
  102. ;environment=A=1,B=2 ; process environment additions
  103. ;serverurl=AUTO ; override serverurl computation (childutils)
  104. ; The below sample group section shows all possible group values,
  105. ; create one or more 'real' group: sections to create "heterogeneous"
  106. ; process groups.
  107. ;[group:thegroupname]
  108. ;programs=progname1,progname2 ; each refers to 'x' in [program:x] definitions
  109. ;priority=999 ; the relative start priority (default 999)
  110. ; The [include] section can just contain the "files" setting. This
  111. ; setting can list multiple files (separated by whitespace or
  112. ; newlines). It can also contain wildcards. The filenames are
  113. ; interpreted as relative to this file. Included files *cannot*
  114. ; include files themselves.
  115. [include]
  116. files = supervisord.d/conf/*.conf

参考:

http://supervisord.org/