supervisor 管理进程, 是通过 fork/exec 的方式将这些被管理的进程当作 supervisor 的子进程来启动,
所以我们只需要将要管理进程的可执行文件的路径添加到 supervisor 的配置文件中就好了.
此时被管理进程被视为 supervisor 的子进程, 若该子进程异常中断, 则父进程可以准确的获取子进程异常中断的信息,
通过在配置文件中设置 autostart=ture, 可以实现对异常中断的子进程的自动重启.
安装 supervisor
$ sudo apt-get install supervisor
配置文件
安装完 supervisor 后, 输入以下命令可得到配置文件:
$ echo_supervisord_conf
或者:
$ cat /etc/supervisord/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
以上配置文件用到几个部分:
[unix_http_server]: 这部分设置 HTTP 服务器监听的 UNIX domain socket
file: 指向 UNIX domain socket, 即 file=/var/run/supervisor.sock
chmod: 启动时改变 supervisor.sock 的权限
[supervisord]: 与 supervisord 有关的全局配置需要在这部分设置
logfile: 指向记录 supervisord 进程的 log 文件
pidfile:pidfile 保存子进程的路径
childlogdir: 子进程 log 目录设为 AUTO 的 log 目录
[supervisorctl]:
serverurl: 进入 supervisord 的 URL, 对于 UNIX domain sockets, 应设为 unix:///absolute/path/to/file.sock
[include]: 如果配置文件包含该部分, 则该部分必须包含一个 files 键:
files: 包含一个或多个文件, 这里包含了 / etc/supervisor/conf.d / 目录下所有的. conf 文件, 可以在该目录下增加我们自己的配置文件, 在该配置文件中增加 [program:x] 部分, 用来运行我们自己的程序, 如下:
[program:x]: 配置文件必须包括至少一个 program,x 是 program 名称, 必须写上, 不能为空
command: 包含一个命令, 当这个 program 启动时执行
directroy: 执行子进程时 supervisord 暂时切换到该目录
user: 账户名
startsecs: 进程从 STARING 状态转换到 RUNNING 状态 program 所需要保持运行的时间(单位: 秒)
redirect_stderr: 如果是 true, 则进程的 stderr 输出被发送回其 stdout 文件描述符上的 supervisord
stdout_logfile: 将进程 stdout 输出到指定文件
stdout_logfile_maxbytes:stdout_logfile 指定日志文件最大字节数, 默认为 50MB, 可以加 KB,MB 或 GB 等单位
stdout_logfile_backups: 要保存的 stdout_logfile 备份的数量
示例如下, 在目录 / etc/supervisor/conf.d / 下创建 awesome.conf, 并加入:
- ;/etc/supervisor/conf.d/awesome.conf
- [program:awesome]
- command = /usr/bin/env python3 /srv/awesome/www/app.py
- directory = /srv/awesome/www
- user = www-data
- startsecs = 3
- redirect_stderr = true
- stdout_logfile_maxbytes = 50MB
- stdout_logfile_backups = 10
- stdout_logfile = /srv/awesome/log/app.log
配置完后, 先进入 / srv/awesome / 目录下创建 log 目录, 之后启动 supervisor:
$ sudo supervisord -c supervisor.conf
supervisor 基本命令(后四个命令可以省略 "-c supervisor.conf"):
supervisord -c supervisor.conf 通过配置文件启动 supervisor
supervisorctl -c supervisor.conf status 查看状态
supervisorctl -c supervisor.conf reload 重新载入配置文件
supervisorctl -c supervisor.conf start [all]|[x] 启动所有 / 指定的程序进程
supervisorctl -c supervisor.conf stop [all]|[x] 关闭所有 / 指定的程序进程
执行服务(运行 app.py):
$ sudo supervisorctl start awesome
如果 supervisor 遇到错误, 可以在 / var/log/supervisor/supervisord.log 中查看日志;
如果 app 运行出现问题, 可以在 / srv/awesome/log/app.log 中查看日志.
来源: http://www.bubuko.com/infodetail-2641032.html