环境
Ubuntu16.04
安装
两种方式,推荐apt
pip install supervisor
sudo apt-get install supervisor
配置
安装后配置文件的路径如下:/etc/supervisor/
安装后目录结构如下,其中两个 flask.conf
文件都是我个人添加:
.
├── conf.d
│ └── flask.conf
├── flask.conf
└── supervisord.conf
1 directory, 3 files
其中 supervisord.conf
是默认的配置文件,conf.d
目录下默认是空的。
可以看一下默认的supervisord.conf
文件,他会包含conf.d
目录下的配置文件:
[include]
files = /etc/supervisor/conf.d/*.conf
我们如何添加自己的配置文件呢?可以在conf.d
目录下添加让supervisord.conf
包含进去,也可以新建一个完整的配置文件,在启动服务的时候选择对应的配置文件。二者选其一就可,这里我说使用的后者。
echo_supervisord_conf > flask.conf
使用该命令会生成默认的配置文件写入目的文件,然后根据我们的需要修改,也可以根据文档直接新建一个配置文件,这样会比较精简一些,下面是我的配置文件内容:
flask.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.
[inet_http_server]
port = 0.0.0.0:9001
username = xxx
password = xxxx
[program:flask]
directory=/home/ubuntu/codes/GA_Flask
command=python3 app.py
autostart=true
autorestart=true
startretries=100
redirect_stderr=true
stdout_logfile=/home/ubuntu/codes/out.log
[include]
files = /etc/supervisor/conf.d/*.conf
PS:一些说明
[program:flask]
是对应的进程配置,参数都很好懂
[inet_http_server]
是对应的web服务的配置,port这里ip要注意配置 0.0.0.0
启动
不指定参数的话使用默认配置文件:supervisord.conf
可以通过 -c 指定配置文件:
supervisord -c /etc/supervisor/flask.conf
如果遇到如下报错:Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting supervisord.
解决方法如下:
下面之中一个
unlink socket /var/run/supervisor.sock
unlink /var/run/supervisor.sock
unlink /tmp/supervisor.sock
这个错误的原因就是supervisor.sock 这个文件会被系统自动删除或者其它原因不存在了,删除软连接就可以了。 supervisor.sock 生成的位置可以去 supervisor 的配置文件中找到。
查看状态
使用web服务查看,也就是之前配置文件里面配置的端口
或者使用命令:supervisorctl
如下:
supervisord : 启动supervisor
supervisorctl reload :修改完配置文件后重新启动supervisor
supervisorctl status :查看supervisor监管的进程状态
supervisorctl start 进程名 :启动XXX进程
supervisorctl stop 进程名 :停止XXX进程
supervisorctl stop all:停止全部进程,注:start、restart、stop都不会载入最新的配置文件。
supervisorctl update:根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启