Supervisor安装与配置

by: 铁乐猫

  • Supervisor官网:http://supervisord.org
  • 使用Python开发,client/server服务
  • Linux/Unix系统进程管理工具,不支持Windows系统。
  • 方便监听进行、启动、停止、重启一个或多个进程。
  • 用Supervisor管理的进程,当一个进程意外被杀死,supervisort监听到进程死后,会自动将它重新拉起,很方便的做到进程自动恢复的功能,不再需要自己写shell脚本来控制。
  • 依赖Python2.4以上版本。

测试环境

  • python2.7
  • openSUSE
    • operation普通用户
  • pip18.0
  • supervisor-3.3.4

安装supervisor

pip install supervisor

supervisor安装完成后会产生三个执行程序:

  • supervisortd
    • 守护进程,用于接收进程管理命令
  • supervisorctl
    • 客户端,用于和守护进程通信,发送管理进程的指令
  • echo_supervisord_conf
    • 生成初始配置文件的程序
/usr/bin/echo_supervisord_conf
/usr/bin/supervisorctl
/usr/bin/supervisord

配置supervisor

运行supervisord服务的时候,需要指定supervisor配置文件,如果没有显示指定,默认在以下目录查找:

$CWD/supervisord.conf
$CWD/etc/supervisord.conf
/etc/supervisord.conf
/etc/supervisor/supervisord.conf
../etc/supervisord.conf (相对于可执行程序的路径)
../supervisord.conf (相对于可执行程序的路径)

$CWD 表示运行supervisord程序的目录路径

运行

echo_supervisord_conf

即可看到默认配置情况,但是一般情况下,我们都不要去修改默认的配置,而是将默认配置重定向到另外的文件中,不同的进程运用不同的配置文件去对默认文件进行复写。

一般是通过运行echo_supervisord_conf程序加上重定向>来生成supervisor的初始化配置文件:

sudo mkdir /etc/supervisor
echo_supervisord_conf > /etc/supervisor/supervisord.conf

我在初始化配置文件时碰到权限拒绝的错误

operation@opensuse-wordpress:/etc/supervisor> sudo echo_supervisord_conf > /etc/supervisor/supervisord.conf
-bash: /etc/supervisor/supervisord.conf: Permission denied

为了方便进行下一步的测试,所以我将配置文件直接建立在operation用户的特定conf文件夹下,而不再强求建在etc目录下。这样就能最大程度保证初始化配置文件不受权限所阻,而且因为后面我要测试的uwsgi服务+django项目也是存放在用户家目录下的,也不会有什么影响。

operation@opensuse-wordpress:/etc> echo_supervisord_conf > /home/operation/work/conf/supervisord.conf

如果有强迫症还是想将配置文件放/etc/supervisor目录下的话,可以将生成的这个初始化配置文件copy一份过去。

默认配置文件

  • 默认的配置文件如下:
    • 要注意的是,supervisord.pid 以及 supervisor.sock是放在 /tmp 目录下,但是 /tmp 目录是存放临时文件,里面的文件是会被 Linux 系统删除的,一旦这些文件丢失,就无法再通过 supervisorctl来执行 restart 和 stop 命令了,会得到 unix:///tmp/supervisor.sock 不存在的错误 。所以建议在配置文件中为pid和sock临时文件指定到比如/var/run之类的目录。
operation@opensuse-wordpress:~> echo_supervisord_conf 
; Sample supervisor config file.
;
; For more information on the config file, please see:
; http://supervisord.org/configuration.html
;
; Notes:
;  - Shell expansion ("~" or "$HOME") is not supported. Environment
;   variables can be expanded using this syntax: "%(ENV_HOME)s".
;  - Quotes around values are not supported, except in the case of
;   the environment= options as shown below.
;  - Comments must have a leading space: "a=b ;comment" not "a=b;comment".
;  - Command will be truncated if it looks like a config file comment, e.g.
;    "command=bash -c 'foo ; bar'" will truncate to "command=bash -c 'foo ".

[unix_http_server]
file=/tmp/supervisor.sock   ; the path to the socket file
;chmod=0700                 ; socket file mode (default 0700)
;chown=nobody:nogroup       ; socket file uid:gid owner
;username=user             ; default is no username (open server)
;password=123               ; default is no password (open server)

;[inet_http_server]         ; inet (TCP) server disabled by default
;port=127.0.0.1:9001       ; ip_address:port specifier, *:port for all iface
;username=user             ; default is no username (open server)
;password=123               ; default is no password (open server)

[supervisord]
logfile=/tmp/supervisord.log ; main log file; default $CWD/supervisord.log
logfile_maxbytes=50MB       ; max main logfile bytes b4 rotation; default 50MB
logfile_backups=10           ; # of main logfile backups; 0 means none, default 10
loglevel=info               ; log level; default info; others: debug,warn,trace
pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid
nodaemon=false               ; start in foreground if true; default false
minfds=1024                 ; min. avail startup file descriptors; default 1024
minprocs=200                 ; min. avail process descriptors;default 200
;umask=022                   ; process file creation umask; default 022
;user=chrism                 ; default is current user, required if root
;identifier=supervisor       ; supervisord identifier, default is 'supervisor'
;directory=/tmp             ; default is not to cd during start
;nocleanup=true             ; don't clean up tempfiles at start; default false
;childlogdir=/tmp           ; 'AUTO' child log dir, default $TEMP
;environment=KEY="value"     ; key value pairs to add to environment
;strip_ansi=false           ; strip ansi escape codes in logs; def. false

; The rpcinterface:supervisor 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:x] sections.

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

; The supervisorctl section configures how supervisorctl will connect to
; supervisord. configure it match the settings in either the unix_http_server
; or inet_http_server section.

[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket
;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
;username=chris             ; should be same as in [*_http_server] if set
;password=123               ; should be same as in [*_http_server] if set
;prompt=mysupervisor         ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history ; use readline history if available

; The sample program section below 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             ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1                   ; number of processes copies to start (def 1)
;directory=/tmp               ; directory to cwd to before exec (def no cwd)
;umask=022                     ; umask for process (default None)
;priority=999                 ; the relative start priority (default 999)
;autostart=true               ; start at supervisord start (default: true)
;startsecs=1                   ; # of secs prog must stay up to be running (def. 1)
;startretries=3               ; max # of serial start failures when starting (default 3)
;autorestart=unexpected       ; when to restart if exited after running (def: unexpected)
;exitcodes=0,2                 ; 'expected' exit codes used with autorestart (default 0,2)
;stopsignal=QUIT               ; signal used to kill process (default TERM)
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false             ; send stop signal to the UNIX process group (default false)
;killasgroup=false             ; SIGKILL the UNIX process group (def 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 (0 means none, 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 (0 means none, 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)

; The sample eventlistener section below shows all possible eventlistener
; subsection values. Create one or more 'real' eventlistener: sections to be
; able to handle event notifications sent by supervisord.

;[eventlistener:theeventlistenername]
;command=/bin/eventlistener   ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1                   ; number of processes copies to start (def 1)
;events=EVENT                 ; event notif. types to subscribe to (req'd)
;buffer_size=10               ; event buffer queue size (default 10)
;directory=/tmp               ; directory to cwd to before exec (def no cwd)
;umask=022                     ; umask for process (default None)
;priority=-1                   ; the relative start priority (default -1)
;autostart=true               ; start at supervisord start (default: true)
;startsecs=1                   ; # of secs prog must stay up to be running (def. 1)
;startretries=3               ; max # of serial start failures when starting (default 3)
;autorestart=unexpected       ; autorestart if exited after running (def: unexpected)
;exitcodes=0,2                 ; 'expected' exit codes used with autorestart (default 0,2)
;stopsignal=QUIT               ; signal used to kill process (default TERM)
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false             ; send stop signal to the UNIX process group (default false)
;killasgroup=false             ; SIGKILL the UNIX process group (def false)
;user=chrism                   ; setuid to this UNIX account to run the program
;redirect_stderr=false         ; redirect_stderr=true is not allowed for eventlisteners
;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 (0 means none, 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=10     ; # of stderr logfile backups (0 means none, 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)

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

;[group:thegroupname]
;programs=progname1,progname2 ; each refers to 'x' in [program:x] definitions
;priority=999                 ; the relative start priority (default 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
  • 摘出常用的配置加以说明如下:
[unix_http_server]
file=/tmp/supervisor.sock  
;UNIX socket 文件,供supervisorctl使用,建议路径改为/var/run/supervisor.sock之类,以避免被系统删除。

;chmod=0700                 ;socket文件的权限,默认是0700
;chown=nobody:nogroup       ;socket文件的属主:属组,格式:uid:gid

;[inet_http_server]         ;HTTP服务器,提供web管理界面,一般默认不开放
;port=127.0.0.1:9001       ;Web管理后台运行的IP和端口,如果开放到公网,需要注意安全性
;username=user             ;登录管理后台的用户名
;password=123               ;登录管理后台的密码

[supervisord]
logfile=/tmp/supervisord.log ;日志文件,默认是 $CWD/supervisord.log
logfile_maxbytes=50MB       ;日志文件大小,超出会自动进行切割rotate,默认 50MB,如果设成0,表示不限制大小
logfile_backups=10           ;日志文件保留备份数量默认10,设为0表示不备份
loglevel=info               ;日志级别,默认info,其它: debug,warn,trace
pidfile=/tmp/supervisord.pid ;pid 文件,同样建议指定到类似/var/run/supervisord.pid
nodaemon=false               ;是否在前台启动,默认是false,即以 daemon 的方式启动
minfds=1024                 ;可以打开的文件描述符的最小值,默认 1024
minprocs=200                 ;可以打开的进程数的最小值,默认 200

[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ;通过UNIX socket连接supervisord,路径与unix_http_server部分的file一致
;serverurl=http://127.0.0.1:9001 ; 通过HTTP的方式连接supervisord

; [program:theprogramname]被管理的进程配置参数,theprogramname是进程的名称
[program:uwsgi_luffy]
command=/usr/local/python-3.5.6/bin/uwsgi --ini /home/operation/work/conf/uwsgi_luffy.ini ; 程序启动命令,可带参数
autostart=true       ; 在supervisord启动的时候也自动启动
startsecs=10         ; 启动10秒后没有异常退出,就表示进程正常启动了,默认为1秒
autorestart=true     ; 程序退出后自动重启,可选值:[unexpected,true,false],默认为unexpected,表示进程意外杀死后才重启
startretries=3       ; 启动失败自动重试次数,默认是3
user=nginx         ; 用哪个用户启动进程,默认是root
priority=999         ; 进程启动优先级,默认999,值小的优先启动
redirect_stderr=true ; 把stderr(标准错误)重定向到stdout(标准输出),默认false
stdout_logfile_maxbytes=20MB ; stdout 日志文件大小,默认50MB
stdout_logfile_backups = 20   ; stdout 日志文件备份数,默认是10
; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)
stdout_logfile=/www/logs/uwsgi_luffy.log
stopasgroup=false     ;默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程
killasgroup=false     ;默认为false,向进程组发送kill信号,包括子进程

;包含其它配置文件
[include]
files = relative/directory/*.ini   ;可以指定一个或多个以.ini结束的配置文件

include示例:

[include]
files = /opt/absolute/filename.ini /opt/absolute/*.ini foo.conf config??.ini

生成配置文件

echo_supervisord_conf > /etc/supervisord.conf

配置管理进程

进程管理配置参数,不建议全都写在supervisord.conf文件中,建议每个进程写一个配置文件放在include指定的目录下,包含进supervisord.conf文件中。

  • 1、创建/etc/supervisor/config.d目录,用于存放进程管理的配置文件
  • 2、 修改/etc/supervisor/supervisord.conf中的include参数,将/etc/supervisor/conf.d目录添加到include中:[include]
    files = /etc/supervisor/config.d/*.ini

启动

supervisord -c /etc/supervisord.conf
知识共享许可协议


本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。

发表评论

电子邮件地址不会被公开。 必填项已用*标注

14 + 18 =