supervisor部署python项目

1. 安装supervisor

supervisor可以帮助我们盯着python程序,哪怕是python程序不小心挂掉了,supervisor也会帮我们立即启起来,首先开始安装

pip install supervisor  -i https://pypi.tuna.tsinghua.edu.cn/simple

2. 生成配置文件

supervisor通过配置文件来控制python文件

 echo_supervisord_conf > ./supervisord.conf

3. 修改配置文件

比如,我的目的是让supervisor保障/test/下的test1.py和test2.py能持续运行

首先通过which python确定使用哪个python

>>> which python
/opt/conda/envs/py374/bin/python
……
;[include]
;files = relative/directory/*.ini

# 直接跳到最后添加如下代码(我的python绝对路径是

/opt/conda/envs/py374/bin/python

)

#项目1
[program:partion-1] # 给个名字
directory=/test/ # python程序所在目录
command= /opt/conda/envs/py374/bin/python test1.py
# 开始等待时间
startsecs=0
# 停止等待时间
stopwaitsecs=0
autorestart=true
# 设置为守护进程
daemon=true

#项目2
[program:partion-2] # 给个名字
directory=/test/ # python程序所在目录
command= /opt/conda/envs/py374/bin/python test2.py
# 开始等待时间
startsecs=0
# 停止等待时间
stopwaitsecs=0
autorestart=true
# 设置为守护进程
daemon=true

4. 首次启动

supervisord -c supervisord.conf

此时test1.py, test2.py 已经开始运行了

5. 后续操作

supervisorctl -c supervisord.conf

停止程序:stop all

启动程序:start all

重载:reload