supervisor进程管理利器

解释

实现进程服务管理,supervisort监听到进程死后,会自动将它重新拉起,很方便的做到进程自动恢复的功能,不再需要自己写shell脚本来控制

安装

包管理工具安装
  root@mysql-2:~# apt-get  install -y supervisor
pip安装
    root@mysql-2:~# pip install supervisor

配置文件介绍

 root@mysql-2:~# cat /etc/supervisor/supervisord.conf
   [unix_http_server]    ; socket 文件,supervisorctl 会使用
      file=/var/run/supervisor.sock   ; (the path to the socket file)
      chmod=0700                       ; sockef file mode (default 0700)
  ;[inet_http_server]         ;HTTP服务器,提供web管理界面
      ;port=127.0.0.1:9001        ;Web管理后台运行的IP和端口,如果开放到公网,需要注意安全性
      ;username=user              ;登录管理后台的用户名
      ;password=123               ;登录管理后台的密码      
   [supervisord]
       logfile=/tmp/supervisord.log ;日志文件,默认是 $CWD/supervisord.log
       logfile_maxbytes=50MB        ;日志文件大小,超出会rotate,默认 50MB,如果设成0,表示不限制大小
       logfile_backups=10           ;日志文件保留备份数量默认10,设为0表示不备份
       loglevel=info                ;日志级别,默认info,其它: debug,warn,trace
       pidfile=/tmp/supervisord.pid ;pid 文件
       nodaemon=false               ;是否在前台启动,默认是false,即以 daemon 的方式启动
       minfds=1024                  ;可以打开的文件描述符的最小值,默认 1024
       minprocs=200                 ;可以打开的进程数的最小值,默认 200
[supervisorctl]
        serverurl=unix:///var/run/supervisor.sock ;通过UNIX socket连接supervisord,路径与unix_http_server部分的file一致
[program:xx]
       command=systemctl restart nginx  ; 程序启动命令
               ; command=/bin/sh -c "exec /usr/local/redis/bin/redis-server /etc/redis/redis.conf"
       autostart=true                             ; 在supervisord启动的时候也自动启动
       startsecs=10                               ; 启动10秒后没有异常退出,就表示进程正常启动了,默认为1秒
       autorestart=true                          ; 程序退出后自动重启,可选值:[unexpected,true,false],默认为unexpected,表示进程意外杀死后才重启
       startretries=3                               ; 启动失败自动重试次数,默认是3
       user=tomcat                                ; 用哪个用户启动进程,默认是root
       priority=999                                 ; 进程启动优先级,默认999,值小的优先启动
       redirect_stderr=true                    ; 把stderr重定向到stdout,默认false
       stdout_logfile_maxbytes=20MB  ; stdout 日志文件大小,默认50MB
       stdout_logfile_backups = 20       ; stdout 日志文件备份数,默认是10
                                                           ; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)
       stdout_logfile=/data/wwwlogs/nginx-access-sup.log
       stopasgroup=false                      ;默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程
       killasgroup=false                         ;默认为false,向进程组发送kill信号,包括子进程
[include]
       files = /etc/supervisor/conf.d/*.conf   ;可以指定一个或多个以.conf结束的配置文件

supervisor 管理nginx

root@mysql-2:/etc/supervisor# cat /etc/supervisor/conf.d/nginx-sup.conf
  [program:nginx]
  process_name=%(program_name)s_%(process_num)02d
  command=systemctl restart nginx
  autostart=true
  autorestart=true
  #startretries=1
  startsecs=0
  stdout_events_enabled=true
  stderr_logfile_maxbytes=20MB
  stdout_logfile_backups = 5     
root@mysql-2:/etc/supervisor# supervisorctl  reload    #重载配置
Restarted supervisord
root@mysql-2:/etc/supervisor# supervisorctl  status    #查看启动任务
nginx:nginx_00                   RUNNING    pid 19191, uptime 0:00:00

supervisor进程管理利器
supervisor进程管理利器

supervisor 监控redis

 root@mysql-2:/etc/supervisor/conf.d# vim redis-sup.conf 
      [program:redis-server]
      command=/bin/sh -c "exec /usr/local/redis/bin/redis-server /etc/redis/redis.conf"
      autostart=true
      autorestart=true
      startretries=2
      startsecs=10
      stdout_events_enabled=true
      stderr_events_enabled=true
      stderr_logfile_maxbytes=20MB
      stdout_logfile_backups = 5

猜你喜欢

转载自blog.51cto.com/9025736/2433204