一、Supervisor简介

    Supervisord是用Python实现的一款非常实用的进程管理工具,类似于monit,monit和supervisord的一个比较大的差异是supervisord管理的进程必须由supervisord来启动,monit可以管理已经在运行的程序;supervisord还要求管理的程序是非daemon程序,supervisord会帮你把它转成daemon程序,因此如果用supervisord来管理nginx的话,必须在nginx的配置文件里添加一行设置daemon off让nginx以非daemon方式启动。
    Supervisor的官方地址:http://supervisord.org/index.html
二、Supervisor的安装
    在这里我使用yum的方式来安装

yum -y install supervisor

    安装之后的一些文件
  1. [root@localhost conf]# rpm ql supervisor
  2. /etc/logrotate.d/supervisor
  3. /etc/rc.d/init.d/supervisord //supervisor的启动文件
  4. /etc/supervisord.conf //supervisor的配置文件
  5. /usr/bin/supervisorctl
  6. /usr/bin/supervisord
  7.  
三、Supervisor的配置文件
  1. [root@localhost ]# cat /etc/supervisord.conf
  2. [supervisord]
  3. http_port=/var/tmp/supervisor.sock ; (default is to run a UNIX domain socket server)
  4. ;http_port=127.0.0.1:9001 ; (alternately, ip_address:port specifies AF_INET)
  5. ;sockchmod=0700 ; AF_UNIX socketmode (AF_INET ignore, default 0700)
  6. ;sockchown=nobody.nogroup ; AF_UNIX socket uid.gid owner (AF_INET ignores)
  7. ;umask=022 ; (process file creation umask;default 022)
  8. logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
  9. logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
  10. logfile_backups=10 ; (num of main logfile rotation backups;default 10)
  11. loglevel=info ; (logging level;default info; others: debug,warn)
  12. pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
  13. nodaemon=false ; (start in foreground if true;default false)
  14. minfds=1024 ; (min. avail startup file descriptors;default 1024)
  15. minprocs=200 ; (min. avail process descriptors;default 200)
  16. ;nocleanup=true ; (don‘t clean up tempfiles at start;default false)
  17. ;http_username=user ; (default is no username (open system))
  18. ;http_password=123 ; (default is no password (open system))
  19. ;childlogdir=/tmp ; (‘AUTO‘ child log dir, default $TEMP)
  20. ;user=chrism ; (default is current user, required if root)
  21. ;directory=/tmp ; (default is not to cd during start)
  22. ;environment=KEY=value ; (key value pairs to add to environment)
  23. [supervisorctl]
  24. serverurl=unix:///var/tmp/supervisor.sock ; use a unix:// URL for a unix socket
  25. ;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
  26. ;username=chris ; should be same as http_username if set
  27. ;password=123 ; should be same as http_password if set
  28. ;prompt=mysupervisor ; cmd line prompt (default “supervisor")
  29. ; The below sample program section shows all possible program subsection values,
  30. ; create one or more ‘real‘ program: sections to be able to control them under
  31. ; supervisor.
  32. ;[program:theprogramname] //使用Supervisor来管理进程一般使用这个字段的内容即可
  33. ;command=/bin/cat ; the program (relative uses PATH, can take args)
  34. ;priority=999 ; the relative start priority (default 999)
  35. ;autostart=true ; start at supervisord start (default: true)
  36. ;autorestart=true ; retstart at unexpected quit (default: true)
  37. ;startsecs=10 ; number of secs prog must stay running (def. 10)
  38. ;startretries=3 ; max # of serial start failures (default 3)
  39. ;exitcodes=0,2 ; ‘expected‘ exit codes for process (default 0,2)
  40. ;stopsignal=QUIT ; signal used to kill process (default TERM)
  41. ;stopwaitsecs=10 ; max num secs to wait before SIGKILL (default 10)
  42. ;user=chrism ; setuid to this UNIX account to run the program
  43. ;log_stdout=true ; if true, log program stdout (default true)
  44. ;log_stderr=true ; if true, log program stderr (def false)
  45. ;logfile=/var/log/cat.log ; child log path, use NONE for none; default AUTO
  46. ;logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
  47. ;logfile_backups=10 ; # of logfile backups (default 10)
  48.  
  49.  
四、使用Supervisor来管理nginx进程
    使用Supervisor来管理nginx进程,nginx必须以非daemon的方式运行
    可以在nginx的配置文件中添加一行配置
  1. daemon off;
    Supervisor的配置文件添加以下内容:
  1. [program:nginx]
  2. command=/app/tengine/sbin/nginx ; the program (relative uses PATH, can take args)
  3. ;priority=999 ; the relative start priority (default 999)
  4. ;autostart=true ; start at supervisord start (default: true)
  5. ;autorestart=true ; retstart at unexpected quit (default: true)
  6. ;startsecs=10 ; number of secs prog must stay running (def. 10)
  7. ;startretries=3 ; max # of serial start failures (default 3)
  8. ;exitcodes=0,2 ; ‘expected’ exit codes for process (default 0,2)
  9. ;stopsignal=QUIT ; signal used to kill process (default TERM)
  10. ;stopwaitsecs=10 ; max num secs to wait before SIGKILL (default 10)
  11. ;user=chrism ; setuid to this UNIX account to run the program
  12. log_stdout=true ; if true, log program stdout (default true)
  13. log_stderr=true ; if true, log program stderr (def false)
  14. logfile=/var/log/supervisor_nginx.log ; child log path, use NONE for none; default AUTO
  15. logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
  16. logfile_backups=10 ; # of logfile backups (default 10)
  17.  
  18.  
    启动Supervisor进程:
  1. supervisord -c /etc/supervisord.conf
    使用supervisorctl来查看被Supervisor管理的进程,也可以通过 http://ip:9001 来查看,下面主要从命令行进行查看
  1. [root@localhost tengine]# supervisorctl
  2. nginx RUNNING pid 8772, uptime 0:51:13
  3. supervisor> help
  4. Documented commands (type help ):
  5. ========================================
  6. EOF exit maintail quit restart start stop
  7. clear help open reload shutdown status tail
  8. supervisor> status
  9. nginx RUNNING pid 8772, uptime 0:51:24
  10. supervisor>
  11.  
   五、扩展
    Supervisor提供非常方便的API,可以结合一些开发项目,集中管理公司内所有被Supervisor管理的进程。

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *

Post Navigation