用supervisor托管python项目
环境
ubuntu16.04
python2.7(不支持3)
安装
apt-get install supervisor
web控制
[inet_http_server]
port=9002
username=nimabi ; (default is no username (open server))
password=yeah 写站点配置文件
nano /etc/supervisor/conf.d/site1.conf
[program:partyjob]
command=uwsgi -i /www/wwwroot/pw.wubowen.com.cn/uwsgi.ini
autostart=true
autorestart=true
user=root
stdout_logfile=/tmp/super-partyjob.log 启动
supervisorctl reload
supervisorctl start xxxxxx
debug
supervisorctl tail django-sspanel-fontend stdout
可以看到日志里出现不了的错误
supervisorctl 下的命令报错时,要优先考虑是不是服务进程没有启动
客户端控制命令
#管理子进程
supervisorctl start/stop/restart
#查看状态
supervisorctl status
#查看子进程的错误输出
supervisorctl tail -f project-name stderr
或者在web端tail -f
#仅重启配置文件有变动的服务
update
#重启daemon
reload
#github issues里关于reload/update/reread/restart的讨论
reread - Reread supervisor configuration. Do not update or restart the running services.
update - Restart service(s) whose configuration has changed. Usually run after 'reread'.
reload - Reread supervisor configuration, reload supervisord and supervisorctl, restart services that were started.
restart - Restart service(s) 深入认识supervisor环境变量
- ubuntu以apt-get 方式安装supervisor后,默认会以服务形式启动supervisor,这时supervisor得不到任何linux系统里的系统变量。所以这时候在子进程的环境变量里配置%(ENV_XXXXX)s引用已有环境变量时,就会出错,导致服务无法启动起来。
- 解决办法是不使用apt-get安装的supervisor,使用python2安装,配置好后,最后在/etc/rc.local加入手工启动的命令
supervisor -c /etc/supervisor/supervisor.conf但是尝试后发现如此开机自启的supervisor依然跟bash没有关系,所以没法获得/etc/profile里的环境变量 - 尝试换个思路,看看uwsgi能不能把系统环境变量带进django。发现有可能能实现,但是查到的资料描述不太清楚。
- 最终的解决方案,是在2的基础上,稍微修改rc.local中的启动命令,通过bash来启动supervisor即可:
bash -c supervisor……
更深入认识supervisor环境变量
You are trying to use an environment variable called PROJECT_ENV, but that variable isn't set in supervisord's environment. If you want to use this variable, it needs to be present in the environment at the time that supervisord is started.
/usr/bin/env可以查看当前环境变量
环境变量必须在supervisor启动前先存在。所以最终操作为修改rc.local
source /etc/profile
supervisord ……
关于supervisorctl
如果supervisord的配置文件在默认路径之中,则直接supervisorctl即可,否则需要-c /…….conf,所以使用alias即可
本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。