zoukankan      html  css  js  c++  java
  • supervisor管理进程工具配置

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

      因为Supervisor是Python开发的,安装前先检查一下系统否安装了Python2.4以上版本。

    1.安装

    1.mac下,可以先安装brew,然后通过brew来安装各种工具
        brew install supervisor
    2.Ubuntu系统下:apt-get install supervisor,通过这种方式安装后,自动设置为开机启动
    3.也可以通过 pip install supervisor 进行安装,但是需要手动启动,然后设置为开机启动(不推荐这种安装方式)

    2.配置

    2.1supervisor配置

    Supervisor 是一个 C/S 模型的程序,supervisord 是 server 端,supervisorctl 是 client 端。

    下面介绍 supervisord 配置方法。supervisord 的配置文件默认位于 /etc/supervisor/supervisord.conf,内容如下(;后面为注释):

    ; supervisor config file
    
    [unix_http_server]
    file=/var/run/supervisor.sock   ; (the path to the socket file) UNIX socket 文件,supervisorctl 会使用
    chmod=0700                       ; sockef file mode (default 0700) socket 文件的 mode,默认是 0700
    
    [supervisord]
    logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log) 日志文件,默认是 $CWD/supervisord.log
    pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid) pid 文件
    childlogdir=/var/log/supervisor            ; ('AUTO' child log dir, default $TEMP)
    
    ; the below section must remain in the config file for RPC
    ; (supervisorctl/web interface) to work, additional interfaces may be
    ; added by defining them in separate rpcinterface: sections
    [rpcinterface:supervisor]
    supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
    
    [supervisorctl]
    serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL  for a unix socket 通过 UNIX socket 连接 supervisord,路径与 unix_http_server 部分的 file 一致
    
    ; 在增添需要管理的进程的配置文件时,推荐写到 `/etc/supervisor/conf.d/` 目录下,所以 `include` 项,就需要像如下配置。
    ; 包含其他的配置文件
    [include]
    files = /etc/supervisor/conf.d/*.conf ; 引入 `/etc/supervisor/conf.d/` 下的 `.conf` 文件

    2.2管理进程配置

    program 的配置文件就写在,supervisord 配置中 include 项的路径下:/etc/supervisor/conf.d/,然后 program 的配置文件命名规则推荐:app_name.conf

    [program:app] ; 程序名称,在 supervisorctl 中通过这个值来对程序进行一系列的操作
    autorestart=True      ; 程序异常退出后自动重启
    autostart=True        ; 在 supervisord 启动的时候也自动启动
    redirect_stderr=True  ; 把 stderr 重定向到 stdout,默认 false
    environment=PATH="/home/app_env/bin"  ; 可以通过 environment 来添加需要的环境变量,一种常见的用法是使用指定的 virtualenv 环境
    command=python server.py  ; 启动命令,与手动在命令行启动的命令是一样的
    user=ubuntu           ; 用哪个用户启动
    directory=/home/app/  ; 程序的启动目录
    stdout_logfile_maxbytes = 20MB  ; stdout 日志文件大小,默认 50MB
    stdout_logfile_backups = 20     ; stdout 日志文件备份数
    ; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)
    stdout_logfile = /data/logs/usercenter_stdout.log

    需要注意:

    • 用 supervisord 管理时,gunicorn 的 daemon 选项需要设置为 False
    • 如果启动命令需要包含workon,修改environment参数:environment=PATH="/home/username/.virtualenvs/myproject/bin"

    案例:

    配置Django项目,启动方式:supervisorctl start test

    [program:test]
    command=/srv/envs/mimas/bin/gunicorn gm_rpcd.wsgi:application --workers=2 -k gevent --bind=0.0.0.0:8088 --user=test --chdir /srv/apps/test/
    environment="指定项目的虚拟环境路径"
    user=test
    stdout_logfile=/data/log/test/supervisor/supervisor.stdout.log
    stdout_logfile_maxbytes=1MB
    stdout_logfile_backups=10
    stderr_logfile=/data/log/test/supervisor/supervisor.stderr.log
    stderr_logfile_maxbytes=1MB
    stderr_logfile_backups=10

    配置celery,启动方式:supervisorctl start test-celery

    [program:test-celery]
    command=/srv/envs/test/bin/celery worker -A user_hierarchy --loglevel=DEBUG -c 2
    directory=/srv/apps/test
    user=test
    stdout_logfile=/data/log/test/supervisor/celery.log
    stderr_logfile=/data/log/test/supervisor/celery_error.log
    startsecs=10
    stopwaitsecs = 180
    stopasgroup=true
    killasgroup=true

    配置celery-beat,启动方式:supervisorctl start test-beat

    [program:test-celerybeat]
    command=/srv/envs/test/bin/celery beat -A user_hierarchy --loglevel=DEBUG  # 启动这个任务的命令
    directory=/srv/apps/test
    user=test
    stdout_logfile=/data/log/test/supervisor/celerybeat.log
    stderr_logfile=/data/log/test/supervisor/celerybeat_error.log
    startsecs=10
    stopwaitsecs = 180
    stopasgroup=true
    killasgroup=true

    3.supervisorctl 

       supervisorctl 是 supervisord 的命令行客户端工具,使用的配置和 supervisord 一样

    supervisorctl status  查看所有进程状态
    supervisorctl stop tomcat 停止某个进程
    supervisorctl start tomcat 启动某个进程
    supervisorctl restart tomcat 重启某个进程

    4.web管理界面配置

    [unix_http_server]
    file=/tmp/supervisor.sock   ;UNIX socket 文件,supervisorctl 会使用
    ;chmod=0700                 ;socket文件的mode,默认是0700
    ;chown=nobody:nogroup       ;socket文件的owner,格式:uid:gid
    
    ;[inet_http_server]         ;HTTP服务器,提供web管理界面
    ;port=127.0.0.1:9001        ;Web管理后台运行的IP和端口,如果开放到公网,需要注意安全性
    ;username=user              ;登录管理后台的用户名
    ;password=123               ;登录管理后台的密码

        出于安全考虑,默认配置是没有开启web管理界面,需要修改supervisord.conf配置文件打开http访权限。

        需要访问,则去掉前面的分号,配置端口即可。

    5.启动supervisor服务

    supervisord -c /etc/supervisor/supervisord.conf

    6.配置开机启动supervisor服务

    6.1配置systemctl服务

       1> 进入/lib/systemd/system目录,并创建supervisor.service文件

    [Unit]
    Description=supervisor
    After=network.target
    
    [Service]
    Type=forking
    ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf
    ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown
    ExecReload=/usr/bin/supervisorctl $OPTIONS reload
    KillMode=process
    Restart=on-failure
    RestartSec=42s
    
    [Install]
    WantedBy=multi-user.target

      2> 设置开机启动

    systemctl enable supervisor.service
    systemctl daemon-reload

      3>修改文件权限为766

    chmod 766 supervisor.service

    6.2配置service类型服务

        参考:https://blog.csdn.net/xyang81/article/details/51555473

  • 相关阅读:
    李彦宏最新演讲:移动互联网的时代已经结束了
    表值函数 详解
    SQL中PIVOT 行列转换
    将WeX5部署到自己的Tomcat服务器上
    Cordova webapp实战开发:(2)认识一下Cordova
    Cordova webapp实战开发:(1)为什么选择 Cordova webapp?
    甲有5套房,不上班,靠收房租生活;乙有1套房,上班赚工资……(启示)
    Ubuntu 16.04下为Android编译OpenCV 3.1.0 Manager
    Dual Camera Info
    OpenCV 3.1
  • 原文地址:https://www.cnblogs.com/vipchenwei/p/9145633.html
Copyright © 2011-2022 走看看