用Supervisord管理进程

来源:互联网 发布:Linux tail查找 编辑:程序博客网 时间:2024/05/16 23:57

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配置

#vi /etc/supervisord.conf[unix_http_server]          ; supervisord的unix socket服务配置file=/tmp/supervisor.sock   ; socket文件的保存目录;chmod=0700                 ; socket的文件权限 (default 0700);chown=nobody:nogroup       ; socket的拥有者和组名;username=user              ; 默认不需要登陆用户 (open server);password=123               ; 默认不需要登陆密码 (open server)[inet_http_server]         ; supervisord的tcp服务配置; Web管理界面设定port=127.0.0.1:9001        ; tcp端口username=user              ; tcp登陆用户password=123               ; tcp登陆密码[supervisord]                ; supervisord的主进程配置logfile=/tmp/supervisord.log ; 主要的进程日志配置logfile_maxbytes=50MB        ; 最大日志体积,默认50MBlogfile_backups=10           ; 日志文件备份数目,默认10loglevel=info                ; 日志级别,默认info; 还有:debug,warn,tracepidfile=/tmp/supervisord.pid ; supervisord的pidfile文件nodaemon=false               ; 是否以守护进程的方式启动minfds=1024                  ; 最小的有效文件描述符,默认1024minprocs=200                 ; 最小的有效进程描述符,默认200;umask=022                   ; 进程文件的umask,默认200;user=chrism                 ; 默认为当前用户,如果为root则必填;identifier=supervisor       ; supervisord的表示符, 默认时'supervisor';directory=/tmp              ; 默认不cd到当前目录;nocleanup=true              ; 不在启动的时候清除临时文件,默认false;childlogdir=/tmp            ; ('AUTO' child log dir, default $TEMP);environment=KEY=value       ; 初始键值对传递给进程;strip_ansi=false            ; (strip ansi escape codes in logs; def. false)[supervisorctl]serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket;username=chris              ; 如果设置应该与http_username相同;password=123                ; 如果设置应该与http_password相同;prompt=mysupervisor         ; 命令行提示符,默认"supervisor";history_file=~/.sc_history  ; 命令行历史纪录; the below section must remain in the config file for RPC; (supervisorctl/web interface) to work, additional interfaces may be; added by defining them in separate rpcinterface: sections[rpcinterface:supervisor]supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface; The below sample eventlistener section shows all possible; eventlistener subsection values, create one or more 'real'; eventlistener: sections to be able to handle event notifications; sent by supervisor. ;[eventlistener:theeventlistenername];command=/bin/eventlistener    ; 运行的程序 (相对使用PATH路径, 可以使用参数);process_name=%(program_name)s ; 进程名表达式,默认为%(program_name)s;numprocs=1                    ; 默认启动的进程数目,默认为1;events=EVENT                  ; event notif. types to subscribe to (req'd);buffer_size=10                ; 事件缓冲区队列大小,默认10;directory=/tmp                ; 在运行前cwd到指定的目录,默认不执行cmd;umask=022                     ; 进程umask,默认None;priority=-1                   ; 程序运行的优先级,默认-1;autostart=true                ; 默认随supervisord自动启动,默认true;autorestart=unexpected        ; whether/when to restart (default: unexpected);startsecs=1                   ; number of secs prog must stay running (def. 1);startretries=3                ; max # of serial start failures (default 3);exitcodes=0,2                 ; 期望的退出码,默认0,2;stopsignal=QUIT               ; 杀死进程的信号,默认TERM;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10);stopasgroup=false             ; 向unix进程组发送停止信号,默认false;killasgroup=false             ; 向unix进程组发送SIGKILL信号,默认false;user=chrism                   ; setuid to this UNIX account to run the program;redirect_stderr=true          ; redirect proc stderr to stdout (default false);stdout_logfile=/a/path        ; stdout log path, NONE for none; default AUTO;stdout_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB);stdout_logfile_backups=10     ; # of stdout logfile backups (default 10);stdout_events_enabled=false   ; emit events on stdout writes (default false);stderr_logfile=/a/path        ; stderr log path, NONE for none; default AUTO;stderr_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB);stderr_logfile_backups        ; # of stderr logfile backups (default 10);stderr_events_enabled=false   ; emit events on stderr writes (default false);environment=A=1,B=2           ; process environment additions;serverurl=AUTO                ; override serverurl computation (childutils)[group:appgroup]programs=wapp,wfapp           ; 任何在[program:x]中定义的x;priority=999                  ; 程序运行的优先级,默认999 ; The [include] section can just contain the "files" setting.  This; setting can list multiple files (separated by whitespace or; newlines).  It can also contain wildcards.  The filenames are; interpreted as relative to this file.  Included files *cannot*; include files themselves. ;[include];files = relative/directory/*.ini; 管理的单个进程的配置,可以添加多个program[program:cobar]; 被监控程序指定的运行脚本; 如果command的栏运行的是shell脚本,那么在shell 脚本启动被监控程序时要用exec修饰。例如:;   echo $RUN_CMD;   eval exec $RUN_CMD; 否则,supervisord停止不了启动的进程.除此之外,shell脚本里不能出现&之类的后台运行符号command=/bin/sh /home/jfy/soft/cobar-server-1.2.7/bin/startup2.sh  ;process_name=%(program_name)s ; 进程名表达式,默认为%(program_name)s;numprocs=1                    ; 默认启动的进程数目,默认为1directory=/home/jfy/soft/cobar-server-1.2.7/bin                    ; 在运行前cwd到指定的目录,默认不执行cmd;umask=022                     ; 进程umask,默认Nonepriority=1                    ; 程序运行的优先级,默认999autostart=true                ; 默认随supervisord自动启动,默认trueautorestart=true              ; 被监控程序异常中断是否自动重启startsecs=5                   ; 被监控程序启动时持续时间startretries=5                ; 被监控程序启动失败重试的次数;exitcodes=0,2                 ; 期望的退出码,默认0,2;stopsignal=QUIT               ; 杀死进程的信号,默认TERM;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10);stopasgroup=false             ; 向unix进程组发送停止信号,默认false;killasgroup=false             ; 向unix进程组发送SIGKILL信号,默认false;user=root                     ; 为运行程序的unix帐号设置setuidredirect_stderr=true          ; 将标准错误重定向到标准输出,默认falsestdout_logfile=/home/jfy/soft/cobar-server-1.2.7/logs/console.log        ; stdou 重定向输出文件;stdout_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB);stdout_logfile_backups=10     ; # of stdout logfile backups (default 10);stdout_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0);stdout_events_enabled=false   ; emit events on stdout writes (default false);stderr_logfile=/a/path        ; stderr log path, NONE for none; default AUTO;stderr_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB);stderr_logfile_backups=10     ; # of stderr logfile backups (default 10);stderr_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0);stderr_events_enabled=false   ; emit events on stderr writes (default false);environment=A=1,B=2           ; process environment additions (def no adds);serverurl=AUTO                ; override serverurl computation (childutils)[program:cardapi]command=/usr1/app/run/CARDAPI /usr1/app/ini/cardapi.ini /usr1/app/log/cardapi.trpriority=1numprocs=1autostart=trueautorestart=true; 配置一组进程,对于类似的program可以通过这种方式添加,避免手工一个个添加;[program:bsapp];command=/usr1/app/run/%(program_name)s_%(process_num)02d wapp /usr1/app/log/%(program_name)s_%(process_num)02d.tr;numprocs=3;process_name=%(program_name)s_%(process_num)02d;autostart=true;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


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

supervisorctl命令行控制程序

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

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


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

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


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

启用浏览器访问效果:



0 0
原创粉丝点击