supervisor的安装及使用
官方文档: http://supervisord.org/
安装使用参考:
- centos: https://blog.csdn.net/weixin_39800080/article/details/107590327
- ubuntu: https://my.oschina.net/u/2396236/blog/1594853
0x00 安装
- centos 7.6:
yum install supervisor
#开机自运行
systemctl enable supervisord.service
安装的版本是 3.4
, 生成的配置文件在 /etc/supervisord.conf
:
; Sample supervisor config file.
[unix_http_server]
file=/var/run/supervisor/supervisor.sock ; (the path to the socket file)
;chmod=0700 ; sockef 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=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10 ; (num of main logfile rotation backups;default 10)
loglevel=info ; (log level;default info; others: debug,warn,trace)
pidfile=/var/run/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 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:///var/run/supervisor/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 http_username if set
;password=123 ; should be same as http_password if set
;prompt=mysupervisor ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history ; use readline history if available
; 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 ; 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)
;autorestart=true ; retstart at unexpected quit (default: true)
;startsecs=10 ; number of secs prog must stay running (def. 1)
;startretries=3 ; max # of serial start failures (default 3)
;exitcodes=0,2 ; 'expected' exit codes for process (default 0,2)
;stopsignal=QUIT ; signal used to kill process (default TERM)
;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
;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_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)
; 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 ; 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)
;autorestart=unexpected ; restart at unexpected quit (default: unexpected)
;startsecs=10 ; number of secs prog must stay running (def. 1)
;startretries=3 ; max # of serial start failures (default 3)
;exitcodes=0,2 ; 'expected' exit codes for process (default 0,2)
;stopsignal=QUIT ; signal used to kill process (default TERM)
;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
;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)
; 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 ; 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 = supervisord.d/*.ini
- ubuntu 20:
sudo apt install supervisor
安装的版本是 4.1
, 生成的配置文件在 /etc/supervisor/supervisord.conf
:
; supervisor config file
[unix_http_server]
file=/var/run/supervisor.sock ; (the path to the socket file)
chmod=0700 ; sockef file mode (default 0700)
[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor ; ('AUTO' child log dir, default $TEMP)
; 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:///var/run/supervisor.sock ; use a unix:// URL for a unix socket
; 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 = /etc/supervisor/conf.d/*.conf
0x01 配置
supervisor的配置文件: centos7.6下(v3.4)是
/etc/supervisord.conf
, ubuntu下(v4.1)是/etc/supervisor/supervisord.conf
. (这个与官方文档说的自3.3.0版本就是后面一种路径不一致)
任务配置项
使用配置中的 [include]
的 files
的定义的配置文件列表去启动进程.
注意: centos下
files
定义的是一个相对目录, 而ubuntu下默认的是绝对目录(其实应该一样的). 且配置文件扩展名不同: centos是.ini
, 而ubuntu是.conf
-
command
: 要执行的命令,可以带参数 -
user
: 执行命令的用户名. 只有当supervisord是以root账号启动时才能切换到其他账户. 如果账户切换失败则该任务无法执行. -
autostart
: 是否在supervisor启动时自动启动该任务 -
autorestart
: 程序退出后是否自动重启。可选值:unexpected, true, false。 设置true
, 表示只要子进程挂掉就无条件重启; -
stdout_logfile
: 进程的stdout日志路径, 比如php中的echo输出. 如果设置为NONE则表示不记录. -
stdout_logfile_maxbytes
: 进程的stdout日志的切割大小, 默认50M
-
stderr_logfile
: 进程的错误日志路径. 如果设置为NONE则表示不记录. -
stderr_logfile_maxbytes
: 进程的错误日志的切割大小, 默认50M
任务配置演示
示例文件为 /etc/supervisor/conf.d/infos.conf
(centos下请使用 .ini
后缀)
[program:infos]
command=php /home/www/infos.php
user=root
autostart=false
autorestart=true
stopasgroup=true
killasgroup=true
stdout_logfile=/home/supervisor_log/infos-out.log
stdout_logfile_maxbytes=10MB
stderr_logfile=/home/supervisor_log/infos-err.log
stderr_logfile_maxbytes=10MB
如果命令执行的php文件有变化, 需要重启指定的任务:
supervisorctl start <program>
0x02 常用命令
supervisord
是服务,supervisorctl
是客户端.
任务操作
查看任务的状态:
# 所有任务的状态
supervisorctl status
supervisorctl status all
# 单个任务的状态
supervisorctl status <program>
停止任务:
# 停止所有任务
supervisorctl stop all
# 停止单个任务
supervisorctl stop <program>
启动任务:
# 启动所有任务
supervisorctl start all
# 启动单个任务
supervisorctl start <program>
重启任务:
# 重启所有任务
supervisorctl restart all
# 重启单个任务:
supervisorctl restart <program>
注意:
restart
不会重新读取配置文件, 只是重启任务的命令
更新任务:
# Reload config and add/remove as necessary, and will restart affected programs
supervisorctl update
supervisorctl update all
如果任务的配置文件未变更, 或者不是autostart的, 则不会重启该任务
服务操作
重启supervisord服务 (Restart the remote supervisord)
supervisorctl reload
重新读取配置 (Reload the daemon’s configuration files, without add/remove (no restarts))
supervisorctl reread
其他命令
查看版本:
supervisord -v
查看帮助:
supervisorctl help <command>
更多命令参考官方文档: http://supervisord.org/running.html#running-supervisorctl
0x03 其他
-
不支持守护进程 , 不要用来监听类似
php-fpm
进程. -
任务日志的切割
centos下, 任务的log文件默认 50M
时自动进行切割. (不过次新的文件名后缀不是最大值, 而是1).
比如, 文件按从新到旧的排序是: "pro.log"(当前log) > "pro.log.1"(次新) > "pro.log.2",....