0%

Python - Supervisor安装配置

1. 安装

1
pip install supervisor

2. 初始化启动配置文件

1
2
echo_supervisord_conf > /etc/supervisord.conf
vim /etc/supervisord.conf

最后面加上下面这两行内容,这样就可以从/etc/supervisor目录下自动加载所有的conf文件了。

1
2
[include]
files = /etc/supervisor/*.conf

3. 新建项目配置文件

1
2
3
mkdir /var/log/supervisor
mkdir /etc/supervisor
vim /etc/supervisor/happydev.conf

内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
[program:happydev]
directory = /var/www/happydev ;
command = /var/www/happydev/venv/bin/gunicorn -w4 -b127.0.0.1:8003 --timeout 120 happy:app ;
autostart = true ;
startsecs = 5 ;
autorestart = true ;
startretries = 3 ;
user = root ;
redirect_stderr = true ;
stdout_logfile_maxbytes = 100MB ;
stdout_logfile_backups = 10 ;
stdout_logfile = /var/log/supervisor/happydev.log ;
environment = HAPPY_CONFIG_FILE="/var/www/happydev/happy/config/staging.py",HAPPY_PATH="/var/www/happydev" ;

这里管理的进程是使用gunicorn启动的Flask项目,关于gunicorn的使用可以参考>>传送门 - Gunicorn安装配置<<

4. 启动suprervisord

1
supervisord -c /etc/supervisord.conf

到这一步supervisor已经正常工作了。


5. 常用命令

5.1 停止supervisor主进程

查看supervisord进程号(pid):

1
ps aux | grep supervisord

关闭进程:

1
kill [pid]

5.2 启动单个项目进程

1
supervisorctl start happydev

5.3 停止单个项目进程

1
supervisorctl stop happydev

注意:使用此命令停止的进程不会被自动重启机制重新启动

5.4 重启单个项目进程

1
supervisorctl restart happydev

5.5 重新加载单个项目

1
2
3
supervisorctl remove happydev # 移除happydev项目
supervisorctl reread # 重新读取所有配置文件
supervisorctl add happydev # 添加happydev项目

5.6 查看所有项目进程的运行状态

1
supervisorctl status