zoukankan      html  css  js  c++  java
  • Supervisor初体验

    学习Supervisor

    小白必看的supervisor教程,帮助认识supervisor,再也不会不知所措

    官网

    Ubuntu下载

    下载命令

    sudo apt-get install supervisor 
    - 安装成功后,supervisor会默认启动
    
    • 通过这种方式安装的supervisor会默认安装在/etc/bin,并且会自动加入到系统服务,随着系统的启动而启动;那么通过系统服务启动supervisor的命令是 systemctl start supervisor,查看supervisor状态的命令是systemctl status supervisor
    • 安装成功之后,会在/etc/supervisor中生成supervisor.conf主配置文件;另一种情况是手动生成的配置文件,命令是echo_supervisord_conf > supervisord.conf命令,这种方式生成的配置信息比较全面

    配置文件介绍

    ; supervisor config file
    
    [unix_http_server]
    file=/var/run/supervisor.sock   ; (the path to the socket file)
    chmod=0700                       ; sockef file mode (default 0700)
    
    [supervisord]
    logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
    pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
    childlogdir=/var/log/supervisor            ; ('AUTO' child log dir, default $TEMP)
    minfds=50000
    minprocs=50000
    
    ; 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
    
    ; The [include] section can just contain the "files" setting.  This
    ; setting can list multiple files (separated by whitespace or
    ; newlines).  It can also contain wildcards.  The filenames are
    ; interpreted as relative to this file.  Included files *cannot*
    ; include files themselves.
    
    [include]
    files = /etc/supervisor/conf.d/*.conf
    
    • supervisor.conf中的语法格式

      - 以 [  ]标示起来的部分是配置参数的分组,[]里的单词可以理解为这一部分的功能概述
      - ;标示注释
      - 比较值得注意
      	[include]
        files = /etc/supervisor/conf.d/*.conf
        这部分的功能是,创建了一个conf.d的文件夹,该文件夹下会有多个.conf的配置文件,用来区分和管理多个进程,当然这里你也可以修改未见的后缀为ini,supervisor会自动的去查找这个文件下的配置文件
      
      

    使用

    • 首先将supervisor.conf中[include]配置存放文件的conf.d文件夹,需要清楚的是,不是非的使用这个名字的文件夹,只要按照files指定的路径能够找得到每个配置文件即可

    • conf.d下创建你的配置文件以.conf结尾,这里同理跟files中的配置对上即可

    • 常用配置信息,具体请根据实际需求来定,下面只是举例

      [program:LotteryCeleryWorker] ;定义你的进程的名字,一定要具有识别性,不要与其他的进程重名
      
      directory= /home/ubuntu/var/service_lottery_server/lotterydraw ; 命令执行的目录,要写绝对路径
      
      command= /home/ubuntu/venv/bin/celery -A mycelery.main worker --loglevel=info   ; 运行程序的命令的绝对路径,常用通过pip安装的的python命令就存在环境的bin目录下;后边如果需要跟启动的项目的文件,也是也写绝对路径
      
      autorestart=true ; 程序意外退出是否自动重启
      autostart=true ; 是否自动启动
      stderr_logfile=/var/log/lottery.err.log ; 错误日志文件
      stdout_logfile=/var/log/lottery.out.log ; 输出日志文件
      
      environment=PYTHONPATH='LotteryCeleryBeat/python3.6/site-packages/' ; 进程环境变量,用的不多,因为在command部分可以指定到具体的命令
      
      user=root ; 进程执行的用户身份
      priority=17 ;优先级,值越高,最后启动,最先被关闭,默认值999
      startsecs=10 ;启动延时执行,默认1秒
      stopsignal=INT ;中断信号									
      
    • 详细参数说明

      ;*为必须填写项
      ;*[program:应用名称]
      [program:cat]
      
      ;*命令路径,如果使用python启动的程序应该为 python /home/test.py, 
      ;不建议放入/home/user/, 对于非user用户一般情况下是不能访问
      command=/bin/cat
      
      ;当numprocs为1时,process_name=%(program_name)s
      ;当numprocs>=2时,%(program_name)s_%(process_num)02d
      process_name=%(program_name)s
      
      ;进程数量
      numprocs=1
      
      ;执行目录,若有/home/supervisor_test/test1.py
      ;将directory设置成/home/supervisor_test
      ;则command只需设置成python test1.py
      ;否则command必须设置成绝对执行目录
      directory=/tmp
      
      ;掩码:--- -w- -w-, 转换后rwx r-x w-x
      umask=022
      
      ;优先级,值越高,最后启动,最先被关闭,默认值999
      priority=999
      
      ;如果是true,当supervisor启动时,程序将会自动启动
      autostart=true
      
      ;*自动重启
      autorestart=true
      
      ;启动延时执行,默认1秒
      startsecs=10
      
      ;启动尝试次数,默认3次
      startretries=3
      
      ;当退出码是0,2时,执行重启,默认值0,2
      exitcodes=0,2
      
      ;停止信号,默认TERM
      ;中断:INT(类似于Ctrl+C)(kill -INT pid),退出后会将写文件或日志(推荐)
      ;终止:TERM(kill -TERM pid)
      ;挂起:HUP(kill -HUP pid),注意与Ctrl+Z/kill -stop pid不同
      ;从容停止:QUIT(kill -QUIT pid)
      ;KILL, USR1, USR2其他见命令(kill -l),说明1
      stopsignal=TERM
      
      stopwaitsecs=10
      
      ;*以root用户执行
      user=root
      
      ;重定向
      redirect_stderr=false
      
      stdout_logfile=/a/path
      stdout_logfile_maxbytes=1MB
      stdout_logfile_backups=10
      stdout_capture_maxbytes=1MB
      stderr_logfile=/a/path
      stderr_logfile_maxbytes=1MB
      stderr_logfile_backups=10
      stderr_capture_maxbytes=1MB
      
      ;环境变量设置
      environment=A="1",B="2"
      
      serverurl=AUTO
      

      参考

    常用命令

    • 常用命令汇总

      supervisorctl status        //查看所有进程的状态
      supervisorctl stop program中设置的项目名称       //停止单个进程
      supervisorctl start program中设置的项目名称       //启动当个进程表
      supervisorctl restart  program中设置的项目名称      //重启单个进程
      supervisorctl update        //配置文件修改后使用该命令加载新的配置
      supervisorctl reload        //重新启动配置中的所有程序
      supervisorctl reread        //检查配置信息是否有逻辑错误
      
    • 如果这是第一次运行supervisor,可以直接使用supervisord -c /etc/supervisord.conf来启动

    • 如果你的机器中已经运行了supervisor,你又新添加了自己的项目

      • 第一步,检查配置信息:使用supervisorctl reread检查你的配置信息是否有明显的逻辑错误,这里supervisor会对你的配置信息进行检查,确保你的配置命令可行,你配置的文件路径是否存在;检查完毕后他会提示你具体的错误信息或者配置信息是否有所变化
      • 第二步,加载配置信息:使用supervisorctl update 加载配置信息,当你的配置文件修改之后,要想生效,也要执行这条命令
      • 第三步,启动你的进程:这里请一定要注意,千万不要千万不要轻易使用supervisorctl reload,因为这条命令是。重新加载所有的supervisor里的配置进程,除非你对supervisor管理的进程了如指掌;个人建议使用supervisorctl start program中设置的项目名称的方式启动你的新进程
    • 对于单个进程的管理

      supervisorctl stop program中设置的项目名称       //停止单个进程
      supervisorctl start program中设置的项目名称       //启动当个进程表
      supervisorctl restart  program中设置的项目名称      //重启单个进程
      
    • 多个进程的管理

      supervisorctl status        //查看所有的进程状态
      supervisorctl start all     //启动所有的进程
      supervisorctl stop all      //停止所有的进程
      supervisorctl reload        //重新启动配置中的所有程序
      
    • 通过supervisor的管理

      supervisord -c supervisor.conf       //通过配置文件启动supervisor
      supervisord shutdown              //停止supervisor的运行
      
      supervisorctl -c supervisor.conf status    //查看状态
      supervisorctl -c supervisor.conf reload     //重新载入配置文件
      supervisorctl -c supervisor.conf start [all]|[x] //启动所有/指定的程序进程
      supervisorctl -c supervisor.conf stop [all]|[x]   //关闭所有/指定的程序进程 
      
      
  • 相关阅读:
    Mybatis 的 xml 文件语法错误,启动项目时控制台一直循环解析但是不打印错误
    在一个由 'L' , 'R' 和 'X' 三个字符组成的字符串(例如"RXXLRXRXL")中进行移动操作。一次移动操作指用一个"LX"替换一个"XL",或者用一个"XR"替换一个"RX"。现给定起始字符串start和结束字符串end,请编写代码,当且仅当存在一系列移动操作使得start可以转换成end时, 返回True。
    【Important】数据库索引原理
    服务化的演变和负载均衡
    【问题集】redis.clients.jedis.exceptions.JedisDataException: ERR value is not an integer or out of range
    【Spring】Spring中用到的设计模式
    【设计模式】责任链模式
    【!Important】Zookeeper用来做什么的,有几种类型的节点
    【!Important】如何保证线程执行的先后顺序
    【!Important】Java线程死锁查看分析方法
  • 原文地址:https://www.cnblogs.com/jjzz1234/p/12219202.html
Copyright © 2011-2022 走看看