supervisor入门教程

2023-11-16

supervisor是什么

是一个客户端/服务器系统,允许其用户在类UNIX操作系统上控制许多进程(官方解释)。

简单点来讲,就是一个监控脚本运行的工具,不过他可以统一化管理,laravel的队列文档上也有相关使用方式方法,例如:

  • 定时脚本的启动、重启、关闭和日志监控
  • swoole的启动、重启、关闭和日志监控 (众所周知,swoole大部分的特性都只能在cli中运行)
  • redis的启动、重启、关闭和日志监控 (redis自身未提供类似phpmyadmin的后台可视化工具)
  • laravel中的队列、一些自动化的脚本、workman等等的脚本

supervisor安装

环境说明

CentOS Linux release 7.5.1804 (Core)

安装

安装时需要使用到easy_install命令,所以我们先安装它
yum install python-setuptools

正式安装supervisor
easy_install supervisor

配置

Supervisor安装后不会自动生成配置文件。
请使用命令echo_supervisord_conf > /etc/supervisor/supervisord.conf来生成配置文件。

默认配置文件需要修改几项,下面是修改后的内容

[root@altc1-mytest1 work]# more /etc/supervisor/supervisord.conf
; Sample supervisor config file.
;
; For more information on the config file, please see:
; http://supervisord.org/configuration.html
;
; Notes:
;  - Shell expansion ("~" or "$HOME") is not supported.  Environment
;    variables can be expanded using this syntax: "%(ENV_HOME)s".
;  - Quotes around values are not supported, except in the case of
;    the environment= options as shown below.
;  - Comments must have a leading space: "a=b ;comment" not "a=b;comment".
;  - Command will be truncated if it looks like a config file comment, e.g.
;    "command=bash -c 'foo ; bar'" will truncate to "command=bash -c 'foo ".

[unix_http_server]
file=/tmp/supervisor.sock   ; the path to the socket file
;chmod=0700                 ; socket file mode (default 0700)
;chown=nobody:nogroup       ; socket file uid:gid owner
;username=user              ; default is no username (open server)
;password=123               ; default is no password (open server)

[inet_http_server]         ; inet (TCP) server disabled by default
port=0.0.0.0:9001        ; ip_address:port specifier, *:port for all iface
username=user              ; default is no username (open server)
password=123               ; default is no password (open server)

[supervisord]
logfile=/tmp/supervisord.log ; main log file; default $CWD/supervisord.log
logfile_maxbytes=50MB        ; max main logfile bytes b4 rotation; default 50MB
logfile_backups=10           ; # of main logfile backups; 0 means none, default 10
loglevel=info                ; log level; default info; others: debug,warn,trace
pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid
nodaemon=false               ; start in foreground if true; default false
minfds=1024                  ; min. avail startup file descriptors; default 1024
minprocs=200                 ; min. avail process descriptors;default 200
;umask=022                   ; process file creation umask; default 022
;user=supervisord            ; setuid to this UNIX account at startup; recommended if root
;identifier=supervisor       ; supervisord identifier, default is 'supervisor'
;directory=/tmp              ; default is not to cd during start
;nocleanup=true              ; don't clean up tempfiles at start; default false
;childlogdir=/tmp            ; 'AUTO' child log dir, default $TEMP
;environment=KEY="value"     ; key value pairs to add to environment
;strip_ansi=false            ; strip ansi escape codes in logs; def. false

; The rpcinterface:supervisor 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:x] sections.

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

; The supervisorctl section configures how supervisorctl will connect to
; supervisord.  configure it match the settings in either the unix_http_server
; or inet_http_server section.

[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket
;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
;username=chris              ; should be same as in [*_http_server] if set
;password=123                ; should be same as in [*_http_server] if set
;prompt=mysupervisor         ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history  ; use readline history if available

; The sample program section below shows all possible program subsection values.
; Create one or more 'real' program: sections to be able to control them under
; supervisor.

;[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)
;startsecs=1                   ; # of secs prog must stay up to be running (def. 1)
;startretries=3                ; max # of serial start failures when starting (default 3)
;autorestart=unexpected        ; when to restart if exited after running (def: unexpected)
;exitcodes=0                   ; 'expected' exit codes used with autorestart (default 0)
;stopsignal=QUIT               ; signal used to kill process (default TERM)
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false             ; send stop signal to the UNIX process group (default false)
;killasgroup=false             ; SIGKILL the UNIX process group (def false)
;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 (0 means none, default 10)
;stdout_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
;stdout_events_enabled=false   ; emit events on stdout writes (default false)
;stdout_syslog=false           ; send stdout to syslog with process name (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 (0 means none, default 10)
;stderr_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;stderr_syslog=false           ; send stderr to syslog with process name (default false)
;environment=A="1",B="2"       ; process environment additions (def no adds)
;serverurl=AUTO                ; override serverurl computation (childutils)

; The sample eventlistener section below shows all possible eventlistener
; subsection values.  Create one or more 'real' eventlistener: sections to be
; able to handle event notifications sent by supervisord.

;[eventlistener:theeventlistenername]
;command=/bin/eventlistener    ; 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)
;events=EVENT                  ; event notif. types to subscribe to (req'd)
;buffer_size=10                ; event buffer queue size (default 10)
;directory=/tmp                ; directory to cwd to before exec (def no cwd)
;umask=022                     ; umask for process (default None)
;priority=-1                   ; the relative start priority (default -1)
;autostart=true                ; start at supervisord start (default: true)
;startsecs=1                   ; # of secs prog must stay up to be running (def. 1)
;startretries=3                ; max # of serial start failures when starting (default 3)
;autorestart=unexpected        ; autorestart if exited after running (def: unexpected)
;exitcodes=0                   ; 'expected' exit codes used with autorestart (default 0)
;stopsignal=QUIT               ; signal used to kill process (default TERM)
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false             ; send stop signal to the UNIX process group (default false)
;killasgroup=false             ; SIGKILL the UNIX process group (def false)
;user=chrism                   ; setuid to this UNIX account to run the program
;redirect_stderr=false         ; redirect_stderr=true is not allowed for eventlisteners
;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 (0 means none, default 10)
;stdout_events_enabled=false   ; emit events on stdout writes (default false)
;stdout_syslog=false           ; send stdout to syslog with process name (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 (0 means none, default 10)
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;stderr_syslog=false           ; send stderr to syslog with process name (default false)
;environment=A="1",B="2"       ; process environment additions
;serverurl=AUTO                ; override serverurl computation (childutils)

; The sample group section below shows all possible group values.  Create one
; or more 'real' group: sections to create "heterogeneous" process groups.

;[group:thegroupname]
;programs=progname1,progname2  ; each refers to 'x' in [program:x] definitions
;priority=999                  ; the relative start priority (default 999)

; 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

备注:上面有一行files = /etc/supervisor/conf.d/*.conf类似于nginx管理子配置文件风格一样,这个地方需要提前执行一下
mkdir -p /etc/supervisor/conf.d/

启动

supervisord -c /etc/supervisor/supervisord.conf

停止

supervisorctl shutdown

重新加载配置文件

supervisorctl reload

实战环节

安装好supervisor之后,就可以找个应用程序托管尝试了,我这里选择的是应用程序就是一个手写的shell程序

模拟应用程序

# more /root/work/hello.sh
echo "start..."
i=0;
while (( $i < 5)) ; 
do echo `date '+%Y-%m-%d %H:%M:%S'` ; 
sleep 1; 
i=$(($i+1)) ; 
done;

具体应用程序配置

Supervisor 以 [program:[your_cli_name]]以每段进程配置文件的开头,your_cli_name 则是你的进程名称,名称会显示在Supervisor后台管理工具和Supervisor cli命令输出上。我们以运行hello.sh为例

[program:hello]
command=sh /root/work/hello.sh

完整的配置如下

# more /etc/supervisor/conf.d/hello_test.conf
[program:hello]
command=sh /root/work/hello.sh
stdout_logfile=/tmp/hello.log
stderr_logfile=/tmp/hello_err.log
autostart=true
autorestart=true
startsecs=10
stopwaitsecs=50
priority=1

备注:command后面的内容不能只写/root/work/hello.sh,前面的sh必须加上哟

执行结果

# more /tmp/hello.log
start...
2019-04-25 02:43:20
2019-04-25 02:43:21
2019-04-25 02:43:22
2019-04-25 02:43:23
2019-04-25 02:43:24
start...
2019-04-25 02:43:26
2019-04-25 02:43:27
2019-04-25 02:43:28
2019-04-25 02:43:29
2019-04-25 02:43:30
start...
2019-04-25 02:43:33
2019-04-25 02:43:34
2019-04-25 02:43:35
2019-04-25 02:43:36
2019-04-25 02:43:37
start...
2019-04-25 02:43:41
2019-04-25 02:43:42
2019-04-25 02:43:43
2019-04-25 02:43:44
2019-04-25 02:43:45

备注:可以看到 supervisor会以子进程的形式执行hello.sh应用程序,hello.sh的逻辑是5秒后全部逻辑执行完成并退出,但是从日志上看下面还有3部分相同的日志,这是由于supervisor管理子进程时,如果发现子进程退出后,再尝试重启3次造成的

子进程配置手册

;*为必须填写项
;*[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

WEB控制台管理

可以通过浏览器查看管理的子进程
在这里插入图片描述

参考文章
https://blog.csdn.net/nbczw8750/article/details/84881501

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

supervisor入门教程 的相关文章

随机推荐

  • java application.yml 配置对象数组

    java application yml 配置对象数组 application yml 配置对象数组 常规对象中获取属性 场景 application yml 配置对象数组 定义配置文件结构 用于定义配置文件的数据结构 打印服务中用到的打印
  • Thinkpad在linux(ubuntu)下修改电池充电阈值,成功解决Thinkpad在Linux下的电池充电问题

    look this for more info http www thinkwiki org wiki Tp smapi 安装tp smapi aptitude install tp smapi dkms modprobe tp smapi
  • Kubernetes弃用Docker的由来和始末

    2020年12月初 Kubernetes在发布v1 20的时候重磅宣称将逐渐弃用Docker 一石激起千层浪 瞬间引爆容器圈 但没想到已经过去两个月时间了 还有文章用UC体误导吃瓜群众 还在学Docker Docker已死 额 累了 毁灭吧
  • mysql join 自己_用JOIN自己更新MySql

    HI我有查詢選擇了主鍵 id 1或外鍵 1的所有行 這是自己的連接 用JOIN自己更新MySql 選擇 SELECT f2 wz AS wz FROM d7x6r magazyn faktura zakupowa f LEFT JOIN S
  • 强化学习——基本概念

    什么是强化学习 强化学习关注与智能体 agent 如何与环境交互中不断学习以完成特定的目标 与有监督学习相比 不需要告诉智能体数据以及对应的标签 学习相应的模型 而是需要智能体在环境中一次次学习 哪些数据对应哪些标签 从而学习规律知道策略
  • Oracle 数据导入*.sql 提示ORA-01950

    今天执行远程Oracle 数据库数据导入时 提示ORA 01950 超出导入文件大小限制 cmd 远程连接oracle 数据库 sqlplus root root1234 192 50 68 246 orcl 导入指定位置的 sql文件 E
  • 双向广度优先搜索(介绍)

    双向广度优先搜索 广度优先搜索遵循从初始结点开始一层层扩展直到找到目标结点的搜索规则 它只能较好地解决状态不是太多的情况 承受力很有限 如果扩展结点较多 而目标结点又处在较深层 采用前文叙述的广度搜索解题 搜索量巨大是可想而知的 往往就会出
  • http请求 405错误

    http请求 405错误 方法不被允许 Method not allowed 405错误常常伴随着POST请求 所有有人会告诉你这些 但是时候他并不能解决你的问题 所以我说一点不一样的 假如你有一个user类 里面有两个属性userName
  • nat技术简介(转载)

    NAT Network Address Translation 网络地址转换 是将IP数据报文头中的IP地址转换为另一个IP地址的过程 在实际应用中 NAT主要用于实现私有网络访问公共网络的功能 这种通过使用少量的公网IP地址代表较多的私网
  • 快速搭建你的api数据交易平台-图文开发教程

    项目背景 如果你需要开发搭建自己的api数据交易平台 并且能在平台上面进行对客户管理 接口管理 套餐管理 账单管理 充值管理 那么下面将来介绍如何使用接口大师这个框架快速进行开发 安装 PhalApi专业版的运行环境要求如下 操作系统 Wi
  • nVidia TK1 基于深度学习框架 Caffe 的物体识别

    By Toradex 胡珊逢 1 简介 深度学习目前正吸引着越来越多人的关注 相关算法框架层出不穷 例如TensorFlow Caffe Keras CNTK Torch7等等 这些算法在数据分析 聚类 识别和预测方面提供了极大的帮助 因此
  • Python爬虫-某网酒店数据

    前言 本文是该专栏的第5篇 后面会持续分享python爬虫案例干货 记得关注 本文以某网的酒店数据为例 实现根据目标城市获取酒店数据 具体思路和方法跟着笔者直接往下看正文详细内容 附带完整代码 正文 地址 aHR0cHM6Ly93d3cuY
  • 基于核概念的KCCA算法

    基于核概念的KCCA算法 1 由CCA算法过渡至KCCA算法 2 KCCA算法的原理与推导 1 由CCA算法过渡至KCCA算法 典型相关分析 CCA 算法是一种标准的统计技术 用于寻找两个最大相关的随机向量的线性投影 CCA算法是一个计算两
  • 字符串初始化赋值

    在C语言中 字符串是当做字符数组来处理的 所以字符串有两种声明方式 一种是字符数组 一种是字符指针 1 直接逐个初始化字符数组 字符数组的初始化 最容易理解的方式就是逐个字符赋给数组中各元素 char str 10 I a m h a p
  • 单片机毕设项目分享 基于stm32的智能电子秤系统 - 物联网 嵌入式 单片机

    文章目录 0 前言 1 简介 2 主要器件 3 实现效果 4 设计原理 4 1 STM32F103C8T6 4 2 HX711压力传感器 5 部分核心代码 6 最后 0 前言 这两年开始毕业设计和毕业答辩的要求和难度不断提升 传统的毕设题目
  • Linux下安装jre

    原文链接 https blog csdn net qq 34368587 article details 79559102 个人收藏教程 侵权联系我删除 现需要项目部署到Linux中 需要配置java运行环境 注 以下测试环境系统为cent
  • 我看Java虚拟机(2)---Java虚拟机内存区域详解

    虚拟机内存区域的组成 直接上图 程序计数器 对于Java方法 用来选取下一条要执行的字节码 对于本地方法 值为空 线程独有 虚拟机栈 执行Java方法 每一层都是一个栈帧 栈帧包括局部变量表 操作数栈 动态链接和方法出口等信息 线程独有 本
  • Vue使用 dhtmlx-gantt 甘特图

    使用心得和一些坑分享出来 下载 npm install dhtmlx gantt save 创建 ganttVue 组件
  • React之生命周期-setState

  • supervisor入门教程

    supervisor是什么 是一个客户端 服务器系统 允许其用户在类UNIX操作系统上控制许多进程 官方解释 简单点来讲 就是一个监控脚本运行的工具 不过他可以统一化管理 laravel的队列文档上也有相关使用方式方法 例如 定时脚本的启动