supervisor简明教程

一、supervisor是什么

Linux的后台进程运行有好几种方法,例如nohup,screen等,但是,如果是一个服务程序,要可靠地在后台运行,我们就需要把它做成daemon,最好还能监控进程状态,在意外结束时能自动重启。

supervisor就是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启。

二、安装及使用

1.安装
pip install supervisor    # supervisor目前只支持python2,但是作为容器来说,并不影响监控程序是python3的程序

2.生成配置文件
echo_supervisord_conf > supervisord.conf     #在当前目录下生成

3.启动supervisor
supervisord -c supervisord.conf   #  -c 用来指定配置文件

4.其他常用命令
supervisorctl -c supervisord.conf   # 启动命令行模式,命令行模式使用的配置文件注意一定要和启动supervisor时的配置文件一致
help : 帮助
update : 重新读取配置文件,并重启
restart all : 重启所有进程,不会重新读取配置文件
start all : 启动所有进程
start <name>  : 启动某个进程
status  : 获取所有进程信息
stop all : 停止所有进程
stop <name> : 停止某个进程 

三、添加应用

常用配置, ; 代表注释

    ; ================================  
    ;  uwsgi supervisor  
    ; ================================  
      
    [program:uwsgi]  ; :后是自定义的名称,在supervisorctl下可以通过  start uwsgi 来启动该进程  
      
    command=/path/to/bin/uwsgi --die-on-term --ini /path/to/uwsgi.ini ; 执行的命令,即在命令行中是如何使用该命令的  --die-on-term keep uwsgi cpu rate low;  
    numprocs=1  ; 启动的进程个数,可以同时启动多个进程
      
    stdout_logfile=/var/log/uwsgi/out.log ; 输出日志位置,目录需要创建,并且如果不是root用户执行需要更改目录权限 chown -R username:username /var/log/uwsgi/out.log,下同  
    stderr_logfile=/var/log/uwsgi/err.log  ; 错误日志位置
    autostart=true   ; 自动启动
    autorestart=true  ; 自动重启   
    priority=997  ; 启动的优先级,数字越大,级别越高,如该进程需要比其他进程先启动,则优先级则设置为更高

 四、更多

参考官方文档:http://www.supervisord.org

原文地址:https://www.cnblogs.com/hiveme/p/8202924.html