zoukankan      html  css  js  c++  java
  • 使用 supervisor 来管理 python 进程(以uwsgi为例)

    一、Supervisor 是什么?

    Supervisor是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统。它可以很方便的监听、启动、停止、重启一个或多个进程。用Supervisor管理的进程,当一个进程意外被杀死,supervisort监听到进程死后,会自动将它重新拉起,很方便的做到进程自动恢复的功能,不再需要自己写shell脚本来控制。

    二、如果你还没安装 nginx 或 uwsgi,请看这里:

    《nginx+uwsgi+bottle python服务器部署》

    三、安装及配置 Supervisor

    安装:

    apt install supervisor

    配置:

    安装完成后,默认配置文件的路径是:/etc/supervisor/supervisord.conf,内容如下:

    现在,我们在 /etc/supervisor/conf.d 目录下增加自己的程序配置(以uwsgi为例):

    [program:digger_uwsgi]
    command=/usr/local/bin/uwsgi --ini ./uwsgi.ini
    directory=/data/webroot/python/digger
    stopasgroup=true
    user=python
    autostart=true
    autorestart=true
    stderr_logfile=/var/log/uwsgi/app/err.log
    stdout_logfile=/var/log/uwsgi/app/out.log

    注:不知道自己的uwsgi目录的话,可以用 which uwsgi 命令查看;

    配置的大概意思如下:

    [program:digger_uwsgi] ;程序名称,终端控制时需要的标识
    command=/usr/local/bin/uwsgi --ini ./uwsgi.ini ;运行程序的命令
    directory=/data/webroot/python/digger ;命令执行的目录
    stopasgroup=true ;停止的时候,把整个进程组杀掉
    user=python ;进程执行的用户身份
    autostart=true ;自动启动
    autorestart=true ;程序意外退出时自动重启
    stderr_logfile=/var/log/uwsgi/app/err.log ;错误日志文件
    stdout_logfile=/var/log/uwsgi/app/out.log ;输出日志文件

    详情的参数看这里:http://www.supervisord.org/configuration.html

    uwsgi.ini 内容如下:

    [uwsgi]
    socket = 127.0.0.1:9090
    chdir = /data/webroot/python/digger/
    wsgi-file = web.py
    master = true

    四、启动 Supervisor

    supervisord -c /etc/supervisor/supervisord.conf

    五、supervisor 常用命令

    supervisorctl -c /etc/supervisor/supervisord.conf ;启动supervisor
    supervisorctl shutdown ;停止supervisor
    supervisorctl reload ;重启supervisor
    supervisorctl restart <application name> ;重启指定应用 supervisorctl stop <application name> ;停止指定应用 supervisorctl start <application name> ;启动指定应用 supervisorctl restart all ;重启所有应用 supervisorctl stop all ;停止所有应用 supervisorctl start all ;启动所有应用 supervisorctl status ;查看运行状态

    六、supervisor 开机自启动

    1)检查是否已设置 supervisor开机启动

    sudo systemctl is-enabled supervisord

    2)如果未设置,则新建设置文件 vim  /lib/systemd/system/supervisord.service

    [Unit]
    Description=Supervisord Daemon
    After=rc-local.service nss-user-lookup.target
    
    [Service]
    Type=forking
    ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf
    ExecStop=/usr/bin/supervisord shutdown
    ExecReload=/usr/bin/supervisord reload
    killMode=process
    Restart=on-failure
    RestartSec=42s
    
    [Install]
    WantedBy=multi-user.target

    注:systemd 详情教程看这里:http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html

    3)启用开机启动

    sudo systemctl enable supervisord

    4)重启服务器或使用以下命令启动 supervisord.service

    sudo systemctl start supervisord.service

    完。

  • 相关阅读:
    Ubuntu 20.04 国内下载eclipse 网速慢解决方案
    Ubuntu20.04+GeForce RTX 2080 SUPER+cuda11.1+cudnn8.0.4+openCV4.4.0编译
    计算机专业考研复试面试----计算机组成原理
    计算机专业考研复试面试----操作系统
    Gabor滤波(个人学习)
    局部保留投影算法(个人学习)
    【转载】matlab常用函数
    latex常见的错误(自己经常出现的)
    latex常用的宏包(转载)
    C语言基础知识
  • 原文地址:https://www.cnblogs.com/tujia/p/12134258.html
Copyright © 2011-2022 走看看