这篇文章上次修改于 1520 天前,可能其部分内容已经发生变化,如有疑问可询问作者。

supervisor 进程管理是一个用Python编写简洁方便的进程管理工具。

安装

ubuntu/debian

apt install Supervisor

python环境

pip install Supervisor

配置

可以使用echo_supervisord_conf生成配置文件

echo_supervisord_conf > /etc/supervisord.conf

自动生成的配置如下:

[unix_http_server]
file=/tmp/supervisor.sock   ; UNIX socket 文件,supervisorctl 会使用
;chmod=0700                 ; socket 文件的 mode,默认是 0700
;chown=nobody:nogroup       ; socket 文件的 owner,格式: 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
logfile_backups=10           ; 日志文件保留备份数量默认 10
loglevel=info                ; 日志级别,默认 info,其它: debug,warn,trace
pidfile=/tmp/supervisord.pid ; pid 文件
nodaemon=false               ; 是否在前台启动,默认是 false,即以 daemon 的方式启动
minfds=1024                  ; 可以打开的文件描述符的最小值,默认 1024
minprocs=200                 ; 可以打开的进程数的最小值,默认 200

; 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 ; 通过 UNIX socket 连接 supervisord,路径与 unix_http_server 部分的 file 一致
;serverurl=http://127.0.0.1:9001 ; 通过 HTTP 的方式连接 supervisord
; 包含其他的配置文件

[include]
files = relative/directory/*.ini    ; 可以是 *.conf 或 *.ini

也可以编辑默认配置文件/etc/supervisor/supervisord.conf

创建进程

进程的配置文件一般放在/etc/supervisor/conf.d

vim /etc/supervisor/conf.d/caddy.conf
[program:caddy]
directory =/etc/caddy       ; 程序的启动目录
command=/usr/local/bin/caddy -log stdout -agree=true -conf=/etc/caddy/Caddyfile        ; 启动命令,可以看出与手动在命令行启动的命令是一样的
process_name=%(program_name)s       ;进程名, 如果要启动多个进程, 则修改修改, 默认为%(program_name)%                
numprocs=1           ; number of processes copies to start (def 1)
autostart = true     ; 在 supervisord 启动的时候也自动启动
startsecs = 1        ; 启动 1 秒后没有异常退出,就当作已经正常启动了
autorestart = true   ; 程序异常退出后自动重启
startretries = 3     ; 启动失败自动重试次数,默认是 3
user = leon          ; 用哪个用户启动
loglevel = info       ; 输出日志级别
redirect_stderr = true          ; 把 stderr 重定向到 stdout,默认 false
stdout_logfile_maxbytes = 20MB  ; stdout 日志文件大小,默认 50MB
stdout_logfile_backups = 10     ; stdout 日志文件备份数
stdout_logfile = /var/logs/usercenter_stdout.log            ; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)
stopasgroup=true            ;这一配置项的作用是:如果supervisord管理的进程px又产生了若干子进程,使用supervisorctl停止px进程,停止信号会传播给px产生的所有子进程,确保子进程也一起停止。这一配置项对希望停止所有进程的需求是非常有用的。  
environment=CADDYPATH=/etc/ssl/caddy         ;可以通过 environment 来添加需要的环境变量,一种常见的用法是修改PYTHONPATH ;process environment additions  

几个例子

[program:some-project]  # program后跟着进程名是必须的
command = /data/apps/some-project/bin/python /data/apps/doraemon/some-project/main.py
autostart = true
autorestart = true  # 服务挂掉会自动重启
loglevel = info  # 输出日志级别
stdout_logfile = /data/log/supervisor/some-project-stdout.log
stderr_logfile = /data/log/supervisor/some-project-stderr.log
stdout_logfile_maxbytes = 500MB
stdout_logfile_backups = 50
stdout_capture_maxbytes = 1MB
stdout_events_enabled = false
[program:caddy]
priority=1
environment=CADDYPATH=/etc/ssl/caddy
directory=/etc/caddy
command=/usr/local/bin/caddy -log stdout -agree=true -conf=/etc/caddy/Caddyfile
autorestart=true

一份配置文件至少需要一个 [program:x]部分的配置,来告诉 supervisord 需要管理那个进程。[program:x]语法中的 x 表示 program name,会在客户端(supervisorctl 或 web 界面)显示,在 supervisorctl 中通过这个值来对程序进行 start、restart、stop 等操作。

启动supervisor服务

使用指定的配置文件启动

supervisord -c /etc/supervisord.conf

使用默认的配置

supervisord

使用systemd启动

systemctl start supervisor
systemctl enable supervisor #启用supervisor

supervisorctl

supervisor管理

supervisorctl status #查看当前supervisor管理的进程状态和运行时间
supervisorctl shutdown #关闭supervisor
supervisor reload #重新加载配置文件

进程管理

supervisor start all #启动所有进程
supervisor stop all #停止所有进程
supervisor restart all #重新启动所有进程
supervisor start program-name #启动program-name为[program:xx]中的xx的进程
supervisor stop program-name #停止这个进程
supervisor restart program-name #重新启动这个进程

参考

supervisor 进程管理
Supervisor使用教程
Supervisor使用教程
使用 supervisor 管理进程
Supervisor 入门教程