侧边栏壁纸
博主头像
王旭阳个人博客博主等级

工欲善其事,必先利其器

  • 累计撰写 111 篇文章
  • 累计创建 28 个标签
  • 累计收到 22 条评论

目 录CONTENT

文章目录

Supervisor安装和基本使用

wxy
wxy
2022-06-10 / 0 评论 / 7 点赞 / 638 阅读 / 5325 字
温馨提示:
本文最后更新于 2023-08-31,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

Supervisor是啥?

Supervisor 是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启。

看之前先明确Supervisor有啥用,究竟能帮到我们什么呢?
1.可以将一些脚本设置为以守护进程的方式一直在后台运行。
2.监控进程的运行情况,面对突发情况能及时预警或者重启,防止出现进程挂掉,影响业务的正常运行

安装

我这里是使用的是debian

# 执行安装即可
apt install supervisor
# 安装完成后 查看版本
root@debian:/etc/supervisor# supervisord -v
4.2.2

安装完成默认目录在/etc/supervisor下面

root@debian:/etc/supervisor# pwd
/etc/supervisor
root@debian:/etc/supervisor# ls
conf.d  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)

    ; 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

可以看到

[include]
files = /etc/supervisor/conf.d/*.conf

我们只需要在conf.d目录下建conf文件即可

使用

cond.d目录下新建一个frps.conf文件

  [program:frp] ;项目名称
  directory=/srv/frp-server  ; 程序的启动目录,项目根目录的上一级
  command=/srv/frp-server/frps -c /srv/frp-server/frps.ini  ; 启动命令 
  ;以下可选配置
  process_name=%(program_name)s_%(process_num)02d
  numprocs = 1         ; 开启的进程数量
  autostart = true     ; 在 supervisord 启动的时候也自动启动
  startsecs = 15        ; 启动 5 秒后没有异常退出,就当作已经正常启动了
  autorestart = true   ; 程序异常退出后自动重启
  startretries = 300     ; 启动失败自动重试次数,默认是 3
  user = root          ; 用哪个用户启动
  redirect_stderr = true  ; 把 stderr 重定向到 stdout,默认 false
  stdout_logfile_maxbytes = 50MB  ; stdout 日志文件大小,默认 50MB
  stdout_logfile_backups = 20     ; stdout 日志文件备份数
  ; stdout 日志文件,需要手动创建目录(supervisord 会自动创建日志文件)
  stdout_logfile = /var/supervisor/log/frps.log
  loglevel=info

其中主要的就是command配置,写的需要变成守护进程的命令。
然后重新加载配置

root@debian:/etc/supervisor/conf.d#  supervisorctl reload
Restarted supervisord
root@debian:/etc/supervisor/conf.d#  supervisorctl status
frp                              RUNNING   pid 218022, uptime 0:00:14

常用命令

supervisorctl status        //查看所有进程的状态
supervisorctl stop frp       //停止fep
supervisorctl start frp     //启动frp
supervisorctl restart frp      //重启frp
supervisorctl update        //配置文件修改后使用该命令加载新的配置
supervisorctl reload        //重新启动配置中的所有程序

注:把frp换成all可以管理配置中的所有进程。直接输入supervisorctl进入supervisorctl的shell交互界面,此时上面的命令不带supervisorctl可直接使用。

7

评论区