Supervisor是一个用 Python 写的 C/S 模型的工具程序,主要用于对进程进行监控与管理,能够很方便的用来启动、重启或关闭进程(不只仅是 Python 进程)。 除了对单个进程的控制,它还能够配置 process groups,对进程进行分组管理。html
Supervisor安装完成后会生成三个可执行程序:supervisortd、supervisorctl、echo_supervisord_conf。python
supervisord 是服务端程序,也是supervisor的守护进程程序,接收客户端supervisorctl发送的命令对用户进程进行控制管理(好比重启、关闭等),监控并重启闪退或异常退出的子进程(被管理的用户进程),把子进程的stderr或stdout记录到日志文件中,生成和处理Event等。web
supervisorctl 是客户端程序,用于和守护进程通讯,发送管理进程的指令。supervisorctl 有一个相似shell的命令行界面,咱们能够利用它来查看子进程状态,启动/中止/重启子进程,获取running子进程的列表等等。shell
echo_supervisord_conf 是配置文件生成程序,用于生成初始配置文件。windows
Supervisor还提供了一个 web 管理界面,咱们能够在这个界面上管理进程。安全
Supervisor 能够运行在 Linux、Mac OS X 等类Unix系统上,但不支持windows系统。如前所述,supervisor 是 Python 编写的,因此安装起来也很方便,能够直接使用以下命令安装:bash
sudo pip install supervisor
Supervisor 至关强大,提供了很丰富的功能,不过咱们可能只须要用到其中一小部分。安装完成以后,能够编写配置文件,来知足本身的需求。为了方便,咱们能够把配置分红两部分:supervisord配置和应用程序(即咱们要管理的程序)配置,咱们先来看看 supervisord 的配置。安装完 supervisor 以后,能够运行 echo_supervisord_conf 命令输出默认的配置项,也能够将其重定向到一个配置文件里:服务器
echo_supervisord_conf > /etc/supervisord.conf
经常使用的配置项有如下这些:app
[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 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 [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
注意,在配置文件中进行注释使用分号,而且分号后必需要有一个空格。socket
应用程序的配置能够都写到 supervisord.conf 文件里,若是要管理的应用程序不少,最好把不一样应用写到不一样的配置文件里。咱们新建一个目录 /etc/supervisor/ 用于存放这些配置文件,相应的,须要把 /etc/supervisord.conf 里 include 部分的的配置修改一下:
[include] files = /etc/supervisor/*.conf
应用程序经常使用的配置项:
[program:myapp]
command=python myapp ; 应用程序启动命令
numprocs=1 ; 应用程序进程数
directory=/home/op ; 应用程序的启动目录
autostart=true ; 在 supervisord 启动时应用程序自动启动
startsecs=5 ; 启动 5 秒后没有异常退出,就看成已经正常启动了
startretries=3 ; 启动失败自动重试次数,默认是 3
autorestart=true ; 程序异常退出后自动重启
user=op ; 用哪一个用户启动进程,默认是root
redirect_stderr=true ; 把 stderr 重定向到 stdout,默认 false
stdout_logfile=/data/logs/myapp.log ; stdout 日志文件,须要注意当指定目录不存在时没法正常启动,因此须要手动建立目录(supervisord 会自动建立日志文件)
stdout_logfile_maxbytes=10MB ; stdout 日志文件大小,默认 50MB
stdout_logfile_backups=10 ; stdout 日志文件备份数
运行supervisord程序命令 :
supervisord -c /etc/supervisord.conf
-c 选项用于显式指定supervisor配置文件,若是没有指定,就在如下目录依次查找:
$CWD/supervisord.conf $CWD/etc/supervisord.conf /etc/supervisord.conf /etc/supervisor/supervisord.conf (since Supervisor 3.3.0) ../etc/supervisord.conf (Relative to the executable) ../supervisord.conf (Relative to the executable)
其中,$CWD表示运行supervisord程序的目录。
查看 supervisord 是否在运行:
ps aux | grep supervisord
特别注意,Supervisor 只能管理在前台运行的程序,因此若是应用程序有后台运行的选项,须要关闭。
Supervisorctl 是 supervisord 的一个命令行客户端工具,启动时须要指定配置文件(与 supervisord相同),不然与 supervisord 同样按照顺序查找配置文件。
supervisorctl -c /etc/supervisord.conf
上面这个命令会进入 supervisorctl 的 shell 界面,而后能够执行如下各命令:
> status # 查看应用程序状态 > stop myapp # 关闭应用程序 > start myapp # 启动应用程序 > restart myapp # 重启应用程序 > reread # 读取有更新(增长)的配置文件,不会启动新添加的程序 > update # 重启配置文件修改过的程序
除了进入 supervisorctl 的 shell 界面,还能够直接在 bash 中运行如下命令:
$ supervisorctl status $ supervisorctl stop myapp $ supervisorctl start myapp $ supervisorctl restart myapp $ supervisorctl reread $ supervisorctl update
直接运行以上命令与在supervisorctl 的 shell 界面执行命令效果相同。
参考: