天天看点

Supervisor使用小记安装默认配置文件在/etc/supervisor/supervisord.conf添加一个程序启动常用命令参考:

Supervisor是一个C/S系统,它可以在类UNIX系统上控制系统进程,由python编写,它提供了大量的功能来实现对进程的管理。

程序的多进程启动,可以配置同时启动的进程数,而不需要一个个启动

程序的退出码,可以根据程序的退出码来判断是否需要自动重启

程序所产生日志的处理

进程初始化的环境,包括目录,用户,umask,关闭进程所需要的信号等等

手动管理进程(开始,启动,重启,查看进程状态)的web界面,和xmlrpc接口

支持的平台

可以运行在大多数类UNIX系统上,比如Linux、MAC OS X、Solaris、FreeBSD。

不支持任何版本的Windows。

支持Python2.4以及之后的版本,但不支持Python3。

安装

pip
pip install supervisor
easy_install
easy_install supervisor
apt-get (Debian/Ubuntu)
apt-get update
apt-get install supervisor
           

默认配置文件在/etc/supervisor/supervisord.conf

yum (Centos)

yum install supervisor

创建配置文件

安装完成后,使用下面这两种方式创建默认的配置文件:

使用root身份创建一个全局配置文件echo_supervisord_conf > /etc/supervisord.conf,supervisord程序在运行后会自动查找并加载此目录配置文件。

在当前目录创建配置文件echo_supervisord_conf > supervisord.conf,

然后使用-c参数在启动时手动指定配置文件:supervisord -c supervisord.conf。

配置文件通常被命名为supervisord.conf,它被supervisord和supervisorctl两者使用。如果程序启动时没有包含-c参数(用于告诉程序加载指定的配置文件名),程序将会按照下列顺序查找supervisord.conf文件,然后使用首个查找到的文件作为结果。

$CWD/supervisord.conf

$CWD/etc/supervisord.conf

/etc/supervisord.conf

默认配置文件echo_supervisord_conf内容:

; Sample supervisor config file.
;
; For more information on the config file, please see:
; http://supervisord.org/configuration.html
;
; Note: shell expansion ("~" or "$HOME") is not supported.  Environment
; variables can be expanded using this syntax: "%(ENV_HOME)s".

[unix_http_server]          ; supervisord的unix socket服务配置
file=/tmp/supervisor.sock   ; socket文件的保存目录
;chmod=                 ; socket的文件权限 (default )
;chown=nobody:nogroup       ; socket的拥有者和组名
;username=user              ; 默认不需要登陆用户 (open server)
;password=               ; 默认不需要登陆密码 (open server)

;[inet_http_server]         ; supervisord的tcp服务配置
;port=:        ; tcp端口
;username=user              ; tcp登陆用户
;password=               ; tcp登陆密码

[supervisord]                ; supervisord的主进程配置
logfile=/tmp/supervisord.log ; 主要的进程日志配置
logfile_maxbytes=MB        ; 最大日志体积,默认MB
logfile_backups=           ; 日志文件备份数目,默认
loglevel=info                ; 日志级别,默认info; 还有:debug,warn,trace
pidfile=/tmp/supervisord.pid ; supervisord的pidfile文件
nodaemon=false               ; 是否以守护进程的方式启动
minfds=                  ; 最小的有效文件描述符,默认
minprocs=                 ; 最小的有效进程描述符,默认
;umask=                   ; 进程文件的umask,默认
;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)

; 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

[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket
;serverurl=http://: ; use an http:// url to specify an inet socket
;username=chris              ; 如果设置应该与http_username相同
;password=                ; 如果设置应该与http_password相同
;prompt=mysupervisor         ; 命令行提示符,默认"supervisor"
;history_file=~/.sc_history  ; 命令行历史纪录

; The below sample program section shows all possible program subsection values,
; create one or more 'real' program: sections to be able to control them under
; supervisor.

;[program:theprogramname]
;command=/bin/cat              ; 运行的程序 (相对使用PATH路径, 可以使用参数)
;process_name=%(program_name)s ; 进程名表达式,默认为%(program_name)s
;numprocs=                    ; 默认启动的进程数目,默认为
;directory=/tmp                ; 在运行前cwd到指定的目录,默认不执行cmd
;umask=                     ; 进程umask,默认None
;priority=                  ; 程序运行的优先级,默认
;autostart=true                ; 默认随supervisord自动启动,默认true
;autorestart=unexpected        ; whether/when to restart (default: unexpected)
;startsecs=                   ; number of secs prog must stay running (def. )
;startretries=                ; max # of serial start failures (default )
;exitcodes=,                 ; 期望的退出码,默认,
;stopsignal=QUIT               ; 杀死进程的信号,默认TERM
;stopwaitsecs=               ; max num secs to wait b4 SIGKILL (default )
;stopasgroup=false             ; 向unix进程组发送停止信号,默认false
;killasgroup=false             ; 向unix进程组发送SIGKILL信号,默认false
;user=chrism                   ; 为运行程序的unix帐号设置setuid
;redirect_stderr=true          ; 将标准错误重定向到标准输出,默认false
;stdout_logfile=/a/path        ; 标准输出的文件路径NONE=none;默认AUTO
;stdout_logfile_maxbytes=MB   ; max # logfile bytes b4 rotation (default MB)
;stdout_logfile_backups=     ; # of stdout logfile backups (default )
;stdout_capture_maxbytes=MB   ; number of bytes in 'capturemode' (default )
;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=MB   ; max # logfile bytes b4 rotation (default MB)
;stderr_logfile_backups=     ; # of stderr logfile backups (default )
;stderr_capture_maxbytes=MB   ; number of bytes in 'capturemode' (default )
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;environment=A=,B=           ; process environment additions (def no adds)
;serverurl=AUTO                ; override serverurl computation (childutils)

; 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=                    ; 默认启动的进程数目,默认为
;events=EVENT                  ; event notif. types to subscribe to (req'd)
;buffer_size=                ; 事件缓冲区队列大小,默认
;directory=/tmp                ; 在运行前cwd到指定的目录,默认不执行cmd
;umask=                     ; 进程umask,默认None
;priority=-                   ; 程序运行的优先级,默认-
;autostart=true                ; 默认随supervisord自动启动,默认true
;autorestart=unexpected        ; whether/when to restart (default: unexpected)
;startsecs=                   ; number of secs prog must stay running (def. )
;startretries=                ; max # of serial start failures (default )
;exitcodes=,                 ; 期望的退出码,默认,
;stopsignal=QUIT               ; 杀死进程的信号,默认TERM
;stopwaitsecs=               ; max num secs to wait b4 SIGKILL (default )
;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=MB   ; max # logfile bytes b4 rotation (default MB)
;stdout_logfile_backups=     ; # of stdout logfile backups (default )
;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=MB   ; max # logfile bytes b4 rotation (default MB)
;stderr_logfile_backups        ; # of stderr logfile backups (default )
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;environment=A=,B=           ; process environment additions
;serverurl=AUTO                ; override serverurl computation (childutils)

; The below sample group section shows all possible group values,
; create one or more 'real' group: sections to create "heterogeneous"
; process groups.

;[group:thegroupname]
;programs=progname1,progname2  ; 任何在[program:x]中定义的x
;priority=                  ; 程序运行的优先级,默认

; 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
           

添加一个程序

以shadowsocks为例,编辑配置文件supervisord.conf,在后面添加新程序

[program:shadowsocks]
command=ssserver -c /etc/shadowsocks.json
autostart=true
autorestart=true
user=nobody
           

启动

使用指定配置文件启动:supervisord -c supervisord.conf

使用默认配置文件启动:supervisord

常用命令

控制命令基本都通过supervisorctl执行,输入help可以看到命令列表。这是一些常用命令:

获得所有程序状态 supervisorctl status

关闭目标程序 supervisorctl stop spider

启动目标程序 supervisorctl start spider

关闭所有程序 supervisorctl shutdown

参考:

Supervisor小试

使用supervisord来管理process

supervisord官方文档