当前位置 主页 > 服务器问题 > Linux/apache问题 >

    Linux中的进程守护supervisor安装配置及使用

    栏目:Linux/apache问题 时间:2019-12-09 20:02

    supervisor是一个很好的守护程序管理工具,配置方面自动启动,日志输出,自动切割日志等等一系列强大功能,下面是在CentOS下安装使用supervisor的记录。

    安装

    # epel源
    yum install epel-release
    # 安装supervisor
    yum install -y supervisor
    # 开机自启动
    systemctl enable supervisord
    # 启动supervisord服务
    systemctl start supervisord 
    Bash

    配置路径

    # 主配置文件
    /etc/supervisord.conf
    # 运行程序配置文件夹
    /etc/supervisord.d/
    Bash

    操作命令

    systemctl stop supervisord
    systemctl start supervisord
    systemctl status supervisord
    # 重新加载配置文件,不影响正在运行的程序
    systemctl reload supervisord
    systemctl restart supervisord
    Bash

    使用测试

    写一个测试脚本test.php,记录启动次数和运行。

    <?php
    try {
      $a = file_get_contents('./times.json');
    } catch (Exception $e) {
      $a = 0;
    }
    $a ++;
    file_put_contents('./times.json', $a);
    echo date('Y-m-d H:i:s') . " 这是第{$a}次启动!!!!" . PHP_EOL;
    
    $i = 1;
    while (1) {
      echo date('Y-m-d H:i:s') . " 第{$i}次输出" . PHP_EOL;
      $i ++;
      sleep(5);
    }

    PHP

    在程序配置文件夹/etc/supervisord.d中添加test.ini:

    [program:test]
    directory=/home/wwwroot/test.cc
    command=php test.php
    autostart=true
    autorestart=true
    stderr_logfile=/home/wwwroot/test.cc/log/error.log
    stdout_logfile=/home/wwwroot/test.cc/log/out.log
    Ini

    上面只是一些必要的基本配置,更详细的配置参考:

    ;[program:theprogramname]
    ;command=/bin/cat       ; the program (relative uses PATH, can take args)
    ;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
    ;numprocs=1          ; number of processes copies to start (def 1)
    ;directory=/tmp        ; directory to cwd to before exec (def no cwd)
    ;umask=022           ; umask for process (default None)
    ;priority=999         ; the relative start priority (default 999)
    ;autostart=true        ; start at supervisord start (default: true)
    ;autorestart=true       ; retstart at unexpected quit (default: true)
    ;startsecs=10         ; number of secs prog must stay running (def. 1)
    ;startretries=3        ; max # of serial start failures (default 3)
    ;exitcodes=0,2         ; 'expected' exit codes for process (default 0,2)
    ;stopsignal=QUIT        ; signal used to kill process (default TERM)
    ;stopwaitsecs=10        ; max num secs to wait b4 SIGKILL (default 10)
    ;user=chrism          ; setuid to this UNIX account to run the program
    ;redirect_stderr=true     ; redirect proc stderr to stdout (default false)
    ;stdout_logfile=/a/path    ; stdout log path, NONE for none; default AUTO
    ;stdout_logfile_maxbytes=1MB  ; max # logfile bytes b4 rotation (default 50MB)
    ;stdout_logfile_backups=10   ; # of stdout logfile backups (default 10)
    ;stdout_capture_maxbytes=1MB  ; number of bytes in 'capturemode' (default 0)
    ;stdout_events_enabled=false  ; emit events on stdout writes (default false)
    ;stderr_logfile=/a/path    ; stderr log path, NONE for none; default AUTO
    ;stderr_logfile_maxbytes=1MB  ; max # logfile bytes b4 rotation (default 50MB)
    ;stderr_logfile_backups=10   ; # of stderr logfile backups (default 10)
    ;stderr_capture_maxbytes=1MB  ; number of bytes in 'capturemode' (default 0)
    ;stderr_events_enabled=false  ; emit events on stderr writes (default false)
    ;environment=A=1,B=2      ; process environment additions (def no adds)
    ;serverurl=AUTO        ; override serverurl computation (childutils)
    Ini