用Supervisord管理进程

Supervisord是用Python实现的一款非常实用的进程管理工具,类似于monit。

Monit和Supervisord的一个比较大的差异是Supervisord管理的进程必须由Supervisord来启动,Monit可以管理已经在运行的程序。

Supervisord还要求管理的程序是非Daemon程序,Supervisord会帮你把它转成Daemon程序,因此如果用Supervisord来管理Nginx的话,必须在Nginx的配置文件里添加一行设置Daemon off让Nginx以非Daemon方式启动。


一、Supervisord安装

        Supervisord可以通过easy_install supervisor在线安装,也可以通过Supervisord官网下载后setup.py install离线安装。


       用easy_install在线安装supervisor:

# yum install python-setuptools

# easy_install supervisor

安装好以后,有两个可执行文件和一个脚本文件
/usr/bin/supervisord             --  supervisor服务守护进程
/usr/bin/supervisorctl           --  supervisor服务控制程序,比如:status/start/stop/restart xx 等

/usr/bin/echo_supervisord_conf   --  supervisor产生配置文件样例

# echo_supervisord_conf > /etc/supervisord.conf  -- 生成配置文件


离线源码安装:

1.安装easy_install

wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz#md5=7df2a529a074f613b509fb44feefe74e

tar zxvf setuptools-0.6c11.tar.gz

cd setuptools-0.6c11

python setup.py install


2.安装meld3

wget http://www.plope.com/software/meld3/meld3-0.6.5.tar.gz

tar zxvf meld3-0.6.5.tar.gz

cd meld3-0.6.5

python setup.py install

cd ..


3.安装elementtree

wget http://effbot.org/media/downloads/elementtree-1.2.7-20070827-preview.zip

unzip elementtree-1.2.7-20070827-preview.zip

cd elementtree-1.2.7-20070827-preview

python setup.py install

cd .. 


4.安装supervisord

wget https://pypi.python.org/packages/source/s/supervisor/supervisor-3.1.1.tar.gz

tar zxvf supervisor-3.0a9.tar.gz

cd supervisor-3.0a9

python setup.py install


安装好以后,有两个可执行文件和一个脚本文件

/usr/local/bin/supervisord             --  supervisor服务守护进程
/usr/local/bin/supervisorctl           --  supervisor服务控制程序,比如:status/start/stop/restart xx 等

/usr/local/bin/echo_supervisord_conf   --  supervisor产生配置文件样例

# echo_supervisord_conf > /etc/supervisord.conf  -- 生成配置文件


二、Supervisor配置

[plain]  view plain  copy
 print ?
  1. #vi /etc/supervisord.conf  
  2.   
  3. [unix_http_server]          ; supervisord的unix socket服务配置  
  4. file=/tmp/supervisor.sock   ; socket文件的保存目录  
  5. ;chmod=0700                 ; socket的文件权限 (default 0700)  
  6. ;chown=nobody:nogroup       ; socket的拥有者和组名  
  7. ;username=user              ; 默认不需要登陆用户 (open server)  
  8. ;password=123               ; 默认不需要登陆密码 (open server)  
  9.   
  10. [inet_http_server]         ; supervisord的tcp服务配置  
  11. ; Web管理界面设定  
  12. port=127.0.0.1:9001        ; tcp端口  
  13. username=user              ; tcp登陆用户  
  14. password=123               ; tcp登陆密码  
  15.   
  16. [supervisord]                ; supervisord的主进程配置  
  17. logfile=/tmp/supervisord.log ; 主要的进程日志配置  
  18. logfile_maxbytes=50MB        ; 最大日志体积,默认50MB  
  19. logfile_backups=10           ; 日志文件备份数目,默认10  
  20. loglevel=info                ; 日志级别,默认info; 还有:debug,warn,trace  
  21. pidfile=/tmp/supervisord.pid ; supervisord的pidfile文件  
  22. nodaemon=false               ; 是否以守护进程的方式启动  
  23. minfds=1024                  ; 最小的有效文件描述符,默认1024  
  24. minprocs=200                 ; 最小的有效进程描述符,默认200  
  25. ;umask=022                   ; 进程文件的umask,默认200  
  26. ;user=chrism                 ; 默认为当前用户,如果为root则必填  
  27. ;identifier=supervisor       ; supervisord的表示符, 默认时'supervisor'  
  28. ;directory=/tmp              ; 默认不cd到当前目录  
  29. ;nocleanup=true              ; 不在启动的时候清除临时文件,默认false  
  30. ;childlogdir=/tmp            ; ('AUTO' child log dir, default $TEMP)  
  31. ;environment=KEY=value       ; 初始键值对传递给进程  
  32. ;strip_ansi=false            ; (strip ansi escape codes in logs; def. false)  
  33.   
  34. [supervisorctl]  
  35. serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket  
  36. ;username=chris              ; 如果设置应该与http_username相同  
  37. ;password=123                ; 如果设置应该与http_password相同  
  38. ;prompt=mysupervisor         ; 命令行提示符,默认"supervisor"  
  39. ;history_file=~/.sc_history  ; 命令行历史纪录  
  40.   
  41. ; the below section must remain in the config file for RPC  
  42. ; (supervisorctl/web interface) to work, additional interfaces may be  
  43. ; added by defining them in separate rpcinterface: sections  
  44. [rpcinterface:supervisor]  
  45. supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface  
  46.   
  47. ; The below sample eventlistener section shows all possible  
  48. ; eventlistener subsection values, create one or more 'real'  
  49. ; eventlistener: sections to be able to handle event notifications  
  50. ; sent by supervisor.  
  51.    
  52. ;[eventlistener:theeventlistenername]  
  53. ;command=/bin/eventlistener    ; 运行的程序 (相对使用PATH路径, 可以使用参数)  
  54. ;process_name=%(program_name)s ; 进程名表达式,默认为%(program_name)s  
  55. ;numprocs=1                    ; 默认启动的进程数目,默认为1  
  56. ;events=EVENT                  ; event notif. types to subscribe to (req'd)  
  57. ;buffer_size=10                ; 事件缓冲区队列大小,默认10  
  58. ;directory=/tmp                ; 在运行前cwd到指定的目录,默认不执行cmd  
  59. ;umask=022                     ; 进程umask,默认None  
  60. ;priority=-1                   ; 程序运行的优先级,默认-1  
  61. ;autostart=true                ; 默认随supervisord自动启动,默认true  
  62. ;autorestart=unexpected        ; whether/when to restart (default: unexpected)  
  63. ;startsecs=1                   ; number of secs prog must stay running (def. 1)  
  64. ;startretries=3                ; max # of serial start failures (default 3)  
  65. ;exitcodes=0,2                 ; 期望的退出码,默认0,2  
  66. ;stopsignal=QUIT               ; 杀死进程的信号,默认TERM  
  67. ;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)  
  68. ;stopasgroup=false             ; 向unix进程组发送停止信号,默认false  
  69. ;killasgroup=false             ; 向unix进程组发送SIGKILL信号,默认false  
  70. ;user=chrism                   ; setuid to this UNIX account to run the program  
  71. ;redirect_stderr=true          ; redirect proc stderr to stdout (default false)  
  72. ;stdout_logfile=/a/path        ; stdout log path, NONE for none; default AUTO  
  73. ;stdout_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)  
  74. ;stdout_logfile_backups=10     ; # of stdout logfile backups (default 10)  
  75. ;stdout_events_enabled=false   ; emit events on stdout writes (default false)  
  76. ;stderr_logfile=/a/path        ; stderr log path, NONE for none; default AUTO  
  77. ;stderr_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)  
  78. ;stderr_logfile_backups        ; # of stderr logfile backups (default 10)  
  79. ;stderr_events_enabled=false   ; emit events on stderr writes (default false)  
  80. ;environment=A=1,B=2           ; process environment additions  
  81. ;serverurl=AUTO                ; override serverurl computation (childutils)  
  82.   
  83. [group:appgroup]  
  84. programs=wapp,wfapp           ; 任何在[program:x]中定义的x  
  85. ;priority=999                  ; 程序运行的优先级,默认999  
  86.    
  87. ; The [include] section can just contain the "files" setting.  This  
  88. ; setting can list multiple files (separated by whitespace or  
  89. ; newlines).  It can also contain wildcards.  The filenames are  
  90. ; interpreted as relative to this file.  Included files *cannot*  
  91. ; include files themselves.  
  92.    
  93. ;[include]  
  94. ;files = relative/directory/*.ini  
  95.   
  96. ; 管理的单个进程的配置,可以添加多个program  
  97. [program:cobar]  
  98. ; 被监控程序指定的运行脚本  
  99. ; 如果command的栏运行的是shell脚本,那么在shell 脚本启动被监控程序时要用exec修饰。例如:  
  100. ;   echo $RUN_CMD  
  101. ;   eval exec $RUN_CMD  
  102. ; 否则,supervisord停止不了启动的进程.除此之外,shell脚本里不能出现&之类的后台运行符号  
  103. command=/bin/sh /home/jfy/soft/cobar-server-1.2.7/bin/startup2.sh    
  104. ;process_name=%(program_name)s ; 进程名表达式,默认为%(program_name)s  
  105. ;numprocs=1                    ; 默认启动的进程数目,默认为1  
  106. directory=/home/jfy/soft/cobar-server-1.2.7/bin                    ; 在运行前cwd到指定的目录,默认不执行cmd  
  107. ;umask=022                     ; 进程umask,默认None  
  108.   
  109. priority=1                    ; 程序运行的优先级,默认999  
  110. autostart=true                ; 默认随supervisord自动启动,默认true  
  111. autorestart=true              ; 被监控程序异常中断是否自动重启  
  112. startsecs=5                   ; 被监控程序启动时持续时间  
  113. startretries=5                ; 被监控程序启动失败重试的次数  
  114. ;exitcodes=0,2                 ; 期望的退出码,默认0,2  
  115. ;stopsignal=QUIT               ; 杀死进程的信号,默认TERM  
  116. ;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)  
  117. ;stopasgroup=false             ; 向unix进程组发送停止信号,默认false  
  118. ;killasgroup=false             ; 向unix进程组发送SIGKILL信号,默认false  
  119. ;user=root                     ; 为运行程序的unix帐号设置setuid  
  120. redirect_stderr=true          ; 将标准错误重定向到标准输出,默认false  
  121. stdout_logfile=/home/jfy/soft/cobar-server-1.2.7/logs/console.log        ; stdou 重定向输出文件  
  122. ;stdout_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)  
  123. ;stdout_logfile_backups=10     ; # of stdout logfile backups (default 10)  
  124. ;stdout_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)  
  125. ;stdout_events_enabled=false   ; emit events on stdout writes (default false)  
  126. ;stderr_logfile=/a/path        ; stderr log path, NONE for none; default AUTO  
  127. ;stderr_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)  
  128. ;stderr_logfile_backups=10     ; # of stderr logfile backups (default 10)  
  129. ;stderr_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)  
  130. ;stderr_events_enabled=false   ; emit events on stderr writes (default false)  
  131. ;environment=A=1,B=2           ; process environment additions (def no adds)  
  132. ;serverurl=AUTO                ; override serverurl computation (childutils)  
  133.   
  134. [program:cardapi]  
  135. command=/usr1/app/run/CARDAPI /usr1/app/ini/cardapi.ini /usr1/app/log/cardapi.tr  
  136. priority=1  
  137. numprocs=1  
  138. autostart=true  
  139. autorestart=true  
  140.   
  141. ; 配置一组进程,对于类似的program可以通过这种方式添加,避免手工一个个添加  
  142. ;[program:bsapp]  
  143. ;command=/usr1/app/run/%(program_name)s_%(process_num)02d wapp /usr1/app/log/%(program_name)s_%(process_num)02d.tr  
  144. ;numprocs=3  
  145. ;process_name=%(program_name)s_%(process_num)02d  
  146. ;autostart=true  
  147. ;autorestart=true</span><span style="font-family: 微软雅黑, 'Microsoft YaHei', Oswald, 'Helvetica Neue', Helvetica, Verdana, arial, sans-serif;">  
(更多配置说明请参考:http://supervisord.org/configuration.html)

三、Supervisor管理

Supervisord安装完成后有两个可用的命令行supervisor和supervisorctl。


启动Supervisord
# supervisord
启动时指定配置文件
# supervisord -c /etc/supervisord.conf


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

supervisorctl命令行控制程序

[plain]  view plain  copy
 print ?
  1. # supervisorctl  
  2. appgroup:wapp                    RUNNING   pid 42981, uptime 0:01:45  
  3. appgroup:wfapp                   RUNNING   pid 42982, uptime 0:01:45  
  4. cardapi                          RUNNING   pid 33133, uptime 1:02:32  
  5. cobar                            RUNNING   pid 6761, uptime 22:43:42  
  6. gearmand                         RUNNING   pid 6743, uptime 22:43:53  
  7. memcached                        RUNNING   pid 6728, uptime 22:44:05  
  8. supervisor> help  
  9. default commands (type help <topic>):  
  10. =====================================  
  11. add    clear  fg        open  quit    remove  restart   start   stop  update   
  12. avail  exit   maintail  pid   reload  reread  shutdown  status  tail  version  
  13.   
  14. stop <name>      停止某一个进程(name),name为[program:cobar]里配置的值,这个示例就是cobar。   
  15. start <name>     启动某个进程   
  16. restart <name>   重启某个进程   
  17. stop <gname>:*   重启属于名为gname这个分组的进程(start,restart同理) ,如:stop appgroup:wapp  
  18. stop all         停止全部进程,注:start、restart、stop都不会载入最新的配置文件。   
  19. reload           载入最新的配置文件,停止原有进程并按新的配置启动、管理所有进程。   
  20. update           根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启。 注意:显示用stop停止掉的进程,用reload或者update都不会自动重启。  

OK,基本的操作就是类似这个了


supervisor还支持以WEB方式监控管理进程
找到[inet_http_server]这一段,修改成

[plain]  view plain  copy
 print ?
  1. [inet_http_server]         ; inet (TCP) server disabled by default  
  2. port=*:9001                ; (ip_address:port specifier, *:port for all iface)  
  3. username=admin             ; (default is no username (open server))  
  4. password=123               ; (default is no password (open server))  
其中port这个字段要各位注意,如果*:9001表示允许所有ip访问,如果指定单个IP可以 xx.xx.xx.xx:9001 这样既可。如果你开启了iptabls记得要在规则里允许port指定的端口号。


然后保存配置,重启supervisord
# supervisord reload

启用浏览器访问效果:



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值