[求助] Python Flask 部署时 supervisor 配置出现问题,不能正常运行,需要 dalao 们指点迷津,谢谢!

[环境] :centos+supervisor+gunicorn+flask

在 /www/blog 目录下,运行是正常的

/www/blog/venv/bin/gunicorn -b 0.0.0.0:5000 -w 4 microblog:app

[状态] :添加到 supervisor 配置中却一直重复在 running

$ supervisorctl status
microblog      RUNNING    pid 12499, uptime 0:00:02

[配置] 如下:

[supervisord]
http_port=/var/tmp/supervisor.sock ; (default is to run a UNIX domain socket server)
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB       ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10          ; (num of main logfile rotation backups;default 10)
loglevel=info               ; (logging level;default info; others: debug,warn)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false              ; (start in foreground if true;default false)
minfds=1024                 ; (min. avail startup file descriptors;default 1024)
minprocs=200                ; (min. avail process descriptors;default 200)

[supervisorctl] serverurl=unix:///var/tmp/supervisor.sock ; use a unix:// URL for a unix socket

[program:microblog] command=/www/blog/venv/bin/gunicorn -b 0.0.0.0:5000 -w 4 microblog:app directory=/www/blog user=root autostart=true autorestart=true stopasgroup=true killasgroup=true


[求助] Python Flask 部署时 supervisor 配置出现问题,不能正常运行,需要 dalao 们指点迷津,谢谢!

20 回复

我无法理解你的问题。

开 ssh,让我上去看看

日志贴出来

不是和 venv 有关系?

没 log 我只能等 dalao 来 review 你的配置了…

话说 running 不是运行中的意思吗?是网站打不开还是

兄弟你用了 venv 你环境变量却没设置啊

不用用 root 去跑非系统程序,不用用 root 去跑非系统程序,不用用 root 去跑非系统程序……
ps: supervisor 官网文档了解一下?

running 有啥问题啊…… running 是正常运行中

/var/tmp 下的日志是空的。。。
小哥,怎么联系。。。
我也觉得是很 venv 有关系,但是我添加了环境变量了,在 venv 在没有进入 venv 的情况下能正常使用。。。在 venv/bin/activate 跟 /etc/profile 都添加了 export FLASK_APP=microblog.py 会不会是这个有误?
running 几秒钟就重复 start,running,block。。。

是的,老哥,的确是不要在 root 下跑非系统程序。。。

这个其实不会执行 active 脚本的,所以我感觉修改 active 脚本没有用处的。
我感觉不行就是得给 supervisor 的 program 配置中加入 environment=PATH="/www/blog/venv/bin" 这样环境配置试试

program: microblog
里面可以加环境变量
加的是 VIRTUAL_ENV
你仔细看看 venv/bin/activate
都干了啥

所有的 Python venv 方案都要改 VIRTUAL_ENV 环境变量


日志找到了。。。原来我看错了,是在 logfile=/var/log/supervisor/supervisord.log

<br>2018-06-29 17:07:08,984 CRIT Running without any HTTP authentication checking<br>2018-06-29 17:07:08,989 INFO daemonizing the process<br>2018-06-29 17:07:08,991 INFO supervisord started with pid 5375<br>2018-06-29 17:07:08,995 INFO spawned: 'microblog' with pid 5376<br>2018-06-29 17:07:09,866 INFO exited: microblog (exit status 3; not expected)<br>2018-06-29 17:07:09,867 INFO received SIGCLD indicating a child quit<br>2018-06-29 17:07:10,870 INFO spawned: 'microblog' with pid 5382<br>2018-06-29 17:07:11,864 INFO exited: microblog (exit status 3; not expected)<br>2018-06-29 17:07:11,865 INFO received SIGCLD indicating a child quit<br>2018-06-29 17:07:13,871 INFO spawned: 'microblog' with pid 5389<br>2018-06-29 17:07:14,576 INFO exited: microblog (exit status 1; not expected)<br>2018-06-29 17:07:14,576 INFO received SIGCLD indicating a child quit<br>2018-06-29 17:07:17,581 INFO spawned: 'microblog' with pid 5396<br>2018-06-29 17:07:18,413 INFO exited: microblog (exit status 1; not expected)<br>2018-06-29 17:07:18,413 INFO received SIGCLD indicating a child quit<br>2018-06-29 17:07:19,414 INFO gave up: microblog entered FATAL state, too many start retries too quickly<br>2018-06-29 17:33:58,601 CRIT Supervisor running as root (no user in config file)<br>2018-06-29 17:33:58,635 INFO localhost:0 - - [29/Jun/2018:09:33:58 +0800] "GET / HTTP/1.0" 200 2204<br>2018-06-29 17:35:16,200 CRIT Supervisor running as root (no user in config file)<br>2018-06-29 17:35:16,232 INFO localhost:0 - - [29/Jun/2018:09:35:16 +0800] "GET / HTTP/1.0" 200 2204<br>2018-06-29 17:37:37,650 CRIT received SIGTERM indicating exit request<br>2018-06-29 17:37:58,299 CRIT Supervisor running as root (no user in config file)<br>2018-06-29 17:37:58,311 INFO /var/tmp/supervisor.sock:Medusa (V1.1.1.1) started at Fri Jun 29 17:37:58 2018<br> Hostname: &lt;unix domain socket&gt;<br> Port:/var/tmp/supervisor.sock<br>2018-06-29 17:37:58,363 CRIT Running without any HTTP authentication checking<br>2018-06-29 17:37:58,365 INFO daemonizing the process<br>2018-06-29 17:37:58,366 INFO supervisord started with pid 5702<br>2018-06-29 17:37:58,368 INFO spawned: 'microblog' with pid 5703<br>2018-06-29 17:37:58,851 INFO exited: microblog (exit status 3; not expected)<br>2018-06-29 17:37:58,852 INFO received SIGCLD indicating a child quit<br>2018-06-29 17:37:59,855 INFO spawned: 'microblog' with pid 5707<br>2018-06-29 17:38:00,681 INFO exited: microblog (exit status 3; not expected)<br>2018-06-29 17:38:00,682 INFO received SIGCLD indicating a child quit<br>2018-06-29 17:38:02,687 INFO spawned: 'microblog' with pid 5713<br>2018-06-29 17:38:03,381 INFO exited: microblog (exit status 3; not expected)<br>2018-06-29 17:38:03,381 INFO received SIGCLD indicating a child quit<br>2018-06-29 17:38:06,386 INFO spawned: 'microblog' with pid 5720<br>2018-06-29 17:38:07,114 INFO exited: microblog (exit status 1; not expected)<br>2018-06-29 17:38:07,114 INFO received SIGCLD indicating a child quit<br>2018-06-29 17:38:08,115 INFO gave up: microblog entered FATAL state, too many start retries too quickly<br>2018-06-29 17:38:53,721 INFO localhost:0 - - [29/Jun/2018:09:38:53 +0800] "POST /RPC2 HTTP/1.0" 200 254<br>2018-06-29 17:38:53,724 INFO localhost:0 - - [29/Jun/2018:09:38:53 +0800] "POST /RPC2 HTTP/1.0" 200 1242<br>2018-06-29 17:38:56,321 INFO localhost:0 - - [29/Jun/2018:09:38:56 +0800] "POST /RPC2 HTTP/1.0" 200 254<br>2018-06-29 17:38:56,325 INFO localhost:0 - - [29/Jun/2018:09:38:56 +0800] "POST /RPC2 HTTP/1.0" 200 1242<br>2018-06-29 17:39:00,612 INFO localhost:0 - - [29/Jun/2018:09:39:00 +0800] "POST /RPC2 HTTP/1.0" 200 254<br>2018-06-29 17:39:00,614 INFO localhost:0 - - [29/Jun/2018:09:39:00 +0800] "POST /RPC2 HTTP/1.0" 200 1242<br>2018-06-29 17:45:58,505 INFO localhost:0 - - [29/Jun/2018:09:45:58 +0800] "POST /RPC2 HTTP/1.0" 200 254<br>2018-06-29 17:45:58,509 INFO localhost:0 - - [29/Jun/2018:09:45:58 +0800] "POST /RPC2 HTTP/1.0" 200 1242<br><br>

微信:defunct

用 pyenv 隔离一套 python 环境,保证 python,venv,程序目录和 supervisor 在同一用户下,这是算是最理想的部署环境。

搞定,mark 一下,走人。

多谢老哥帮助!!!
其实是跟 老哥你的想法一致,也跟上面的各位 dalao 想法一致的。
我把 老哥解决方法说一下,以便后面的同学能及时处理这个问题。

原话:
原因是 venv 跟 gunicorn 必须是同一个 shell 引导,否则无法继承环境变量。

解决方法:
<br># cat /etc/supervisord.conf<br>[supervisord]<br>http_port=/var/tmp/supervisor.sock ; (default is to run a UNIX domain socket server)<br>logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)<br>logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)<br>logfile_backups=10 ; (num of main logfile rotation backups;default 10)<br>loglevel=info ; (logging level;default info; others: debug,warn)<br>pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)<br>nodaemon=false ; (start in foreground if true;default false)<br>minfds=1024 ; (min. avail startup file descriptors;default 1024)<br>minprocs=200 ; (min. avail process descriptors;default 200)<br><br>[supervisorctl]<br>serverurl=unix:///var/tmp/supervisor.sock ; use a unix:// URL for a unix socket<br><br>[program:microblog]<br>command=/www/blog/venv/bin/gunicorn_start<br>directory=/www/blog<br>user=root<br>autostart=true<br>autorestart=true<br>stopasgroup=true<br>killasgrouv=true<br>stdout_logfile=/www/blog/logs/microblog.log<br>redirect_stderr=true<br><br><br># cat /www/blog/venv/bin/gunicorn_start<br>#!/bin/sh<br><br>export FLASK_APP=<a target="_blank" href="http://microblog.py" rel="nofollow noopener">microblog.py</a><br><br>cd /www/blog/<br>source venv/bin/activate<br>/www/blog/venv/bin/gunicorn -b localhost:3000 -w 4 microblog:app<br><br>
直接通过一个 sh 脚本处理同一个 shell 引导,
这个时候由 gunicorn 启动的 microblog 应用服务器现在运行在本地端口 3000,
然后我再设置一下 Nginx 就可以将应用程序暴露给外部世界。

回到顶部