美文网首页
supervisor进程管理

supervisor进程管理

作者: jinjin1009 | 来源:发表于2021-04-23 14:36 被阅读0次

    一、背景

    supervisor是一个python写的进程管理工具,可以很方便来启动、重启、关闭进程。

    优点:除了对单个进程的控制,还可以同时启动、关闭多个进程,当服务器出现问题导致所有的应用程序都被杀死后,此时可以用supervisor同时启动所有应用程序而不是一个一个敲命令去启动

    二、supervisord配置

    1、首先是安装 supervisor,通过pip进行安装,之后运行 echo_supervisord_conf 命令输出默认的配置项

    echo_supervisord_conf > /etc/supervisord.conf
    

    2、配置中,supervisord是server端,对应的supervisorctl 是client端,还有相应的应用程序

    下面是supervisord的配置

    [supervisord]
    logfile=/tmp/supervisord.log ; 日志文件,默认是 $CWD/supervisord.log
    logfile_maxbytes=50MB        ; 日志文件大小,超出会 rotate,默认 50MB
    logfile_backups=10           ; 日志文件保留备份数量默认 10
    loglevel=info                ; 日志级别,默认 info,其它: debug,warn,trace
    pidfile=/tmp/supervisord.pid ; pid 文件
    nodaemon=false               ; 是否在前台启动,默认是 false,即以 daemon 的方式启动
    silent=false                 ; no logs to stdout if true; default false
    minfds=1024                  ; 可以打开的文件描述符的最小值,默认 1024
    minprocs=200                 ; 可以打开的进程数的最小值,默认 200
    
    ; 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
    

    inet_http_server 指的是怎么在浏览器界面去访问

    [inet_http_server]         ; inet (TCP) server disabled by default
    port=*:8002                ; ip_address:port specifier, *:port for all iface
    username=dangjinjin              ; default is no username (open server)
    password=dangjinjin123@               ; default is no password (open server)
    

    supervisorctl指的是client端,配置如下

    注意:supervisorctl指的是在终端上敲命令怎么去实现,inet_http_server指的是在浏览器上怎么去访问,两者是不同的客户端访问方式,但是访问的都是一套后端服务 supervisord
    所以需要保证两者的鉴权方式和访问的端口等等均是一致的

    [supervisorctl]
    serverurl=http://localhost:8002 ; use an http:// url to specify an inet socket
    username=dangjinjin              ; should be same as in [*_http_server] if set
    password=dangjinjin123@                ; should be same as in [*_http_server] if set
    prompt=mysupervisor         ; cmd line prompt (default "supervisor")
    

    应用程序配置

    其中[ program:hisearch ]里面的hisearch是应用程序的唯一标识,不能重复,对该程序的所有操作(start, restart 等)都通过名字来实现

    有时候用 Supervisor 托管的程序还会有子进程(如 监控任务),如果只杀死主进程,子进程就可能变成孤儿进程。通过这两项配置来确保所有子进程都能正确停止:

    [program:hisearch]
    command=sh /root/sched_task/baidu/search/test/search_monitor/hisearch_case/bin/run.sh
    autostart=true                ; 在 supervisord 启动的时候也自动启动start at supervisord start (default: true)
    startsecs=5                   ; 启动 5 秒后没有异常退出,就当作已经正常启动了# of secs prog must stay up to be running (def. 1)
    stopasgroup=true             ; send stop signal to the UNIX process group (default false)
    killasgroup=true             ; SIGKILL the UNIX process group (def false)
    user=root                   ; setuid to this UNIX account to run the program
    stdout_logfile=/root/sched_task/baidu/search/test/search_monitor/hisearch_case/logs/hisearch.log
    stderr_logfile=/root/sched_task/baidu/search/test/search_monitor/hisearch_case/logs/hisearch.log.err
    

    三、运行

    将上面的配置保存到/etc/supervisor/supervisord.conf 这个配置文件里面,运行如下命令开启服务端服务 supervisord

    supervisord -c /etc/supervisor/supervisord.conf
    

    查看 supervisord 是否在运行

    ps aux | grep supervisord
    

    Supervisorctl 是 supervisord 的一个命令行客户端工具,启动时需要指定与 supervisord 使用同一份配置文件

    supervisorctl -c /etc/supervisor/supervisord.conf
    

    上面这个命令会进入 supervisorctl 的 shell 界面,然后可以执行不同的命令了:

    > status    # 查看程序状态
    > stop hisearch   # 关闭 hisearch 程序
    > start hisearch  # 启动 hisearch 程序
    > restart hisearch    # 重启 hisearch 程序
    > reread    # 读取有更新(增加)的配置文件,不会启动新添加的程序
    > update    # 重启配置文件修改过的程序
    

    四、遇到的问题

    1、supervisor ERROR (spawn error)现象

    原因: supervisord.conf配置文件不正确导致的

    2、supervisorctl refused connection 现象

    原因:[ supervisorctl ]处的端口,鉴权方式和 [inet_http_server] 处的不一致,需要保持一致

    3、【FATAL 或BACKOFF】 Exited too quickly (process log may have details)

    原因:将supervisord.conf文件里面每个program里面的startsecs由原来的5改为10

    参考文件:http://liyangliang.me/posts/2015/06/using-supervisor/

    相关文章

      网友评论

          本文标题:supervisor进程管理

          本文链接:https://www.haomeiwen.com/subject/mnmcrltx.html