侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Linux系统下的进程监控工具Supervisor的基本使用方法

linux  /  管理员 发布于 7年前   148

Supervisor是一个C/S系统,它允许用户在类UNIX系统上控制一些进程。它具有以下特性:

    1    简单

            Supervisor通过INI格式配置文件进行配置,很容易掌握,它为每个进程提供了很多配置选项,可以使你很容易的重启进程或者自动的轮转日志。

    2    统一

            Supervisor提供了一种统一的方式来start、stop、monitor你的进程, 进程可以单独控制,也可以成组的控制。你可以在本地或者远程命令行或者web接口来配置Supervisor。

    3    有效

            Supervisor通过fork/exec启动它的子进程,子进程并不是守护进程。当一个进程终止的时候,操作系统会立即给Supervisor发送一个信号,而不是像其他解决方案依赖PID文件。

    4    可扩展

            Supervisor包含一个简单的事件通知协议,因此任何程序都可以监控它,而且提供一个XML-RPC控制接口。

    5    兼容

            除了windows平台,其他平台都可运行。

Supervisor系统的组件:

    supervisord:

        服务会启动supervisord服务,它负责调用自己启动子程序,响应来自客户端的命令,重启crash或者退出的进程,记录进程的输出信息,收集事件信息。该服务的配置文件在/etc/supervisor/supervisord.conf

    supervisorctl:

        客户端的命令行工具,提供一个类shell接口,通过它你可以连接到不同的supervisord进程上来管理它们各自的子程序。客户端命令通过UNIX socket或者TCP来和服务通讯,服务端可以要求客户端提供身份验证之后才能进行操作([supervisorctl])。

    Web Server:

        一个小的web接口被集成进了supervisorctl,重启supervisord之后就可以访问了([inet_http_server])。   

    XML-RPC Interface:

        就像HTTP提供WEB UI一样,同时还提供了XML-RPC接口来控制supervisor和由它运行的程序。

安装:

        supervisor是python编写的,显然用easy_install、pip都可以安装,我懒,直接apt-get了,在ubuntu14.04下安装完后版本是3.0b2。

Supervisor服务的启动

        其实启动Supervisor很简单,supervisord -h看看就知道了,最简单的-c根配置文件即可:


复制代码代码如下:
supervisord -- run a set of applications as daemons.

Usage: /usr/bin/supervisord [options]

Options:
-c/--configuration FILENAME -- configuration file
-n/--nodaemon -- run in the foreground (same as 'nodaemon true' in config file)
-h/--help -- print this usage message and exit
-v/--version -- print supervisord version number and exit
-u/--user USER -- run supervisord as this user (or numeric uid)
-m/--umask UMASK -- use this umask for daemon subprocess (default is 022)
-d/--directory DIRECTORY -- directory to chdir to when daemonized
-l/--logfile FILENAME -- use FILENAME as logfile path
-y/--logfile_maxbytes BYTES -- use BYTES to limit the max size of logfile
-z/--logfile_backups NUM -- number of backups to keep when max bytes reached
-e/--loglevel LEVEL -- use LEVEL as log level (debug,info,warn,error,critical)
-j/--pidfile FILENAME -- write a pid file for the daemon process to FILENAME
-i/--identifier STR -- identifier used for this instance of supervisord
-q/--childlogdir DIRECTORY -- the log directory for child process logs
-k/--nocleanup --  prevent the process from performing cleanup (removal of
                   old automatic child log files) at startup.
-a/--minfds NUM -- the minimum number of file descriptors for start success
-t/--strip_ansi -- strip ansi escape codes from process output
--minprocs NUM  -- the minimum number of processes available for start success
--profile_options OPTIONS -- run supervisord under profiler and output
                             results based on OPTIONS, which  is a comma-sep'd
                             list of 'cumulative', 'calls', and/or 'callers',
                             e.g. 'cumulative,callers')
        不过既然我这懒人是用apt-get安装的,那安装包的规范必然符合debian系的风格了,直接service supervisor start即可启动,且慢,我们还没配置supervisor的配置文件呢,启动了也没什么效果。我们后面详解配置文件的配置。

supervisorctl客户端的使用

        supervisorctl有两种模式,一种是交互模式,一种是命令行模式。在命令行输入supervisorctl直接回车,即可进入交互模式。

复制代码代码如下:
supervisorctl -- control applications run by supervisord from the cmd line.

Usage: /usr/bin/supervisorctl [options] [action [arguments]]
 
Options:
-c/--configuration -- configuration file path (default /etc/supervisor.conf)
-h/--help -- print usage message and exit
-i/--interactive -- start an interactive shell after executing commands
-s/--serverurl URL -- URL on which supervisord server is listening
     (default "http://localhost:9001"). 
-u/--username -- username to use for authentication with server
-p/--password -- password to use for authentication with server
-r/--history-file -- keep a readline history (if readline is available)

复制代码代码如下:
action [arguments] -- see below

Actions are commands like "tail" or "stop".  If -i is specified or no action is
specified on the command line, a "shell" interpreting actions typed
interactively is started.  Use the action "help" to find out about available
actions.
Supervisor的开机自启动

        如果你是pip或者easy_install安装的,开机服务自启动还真是个麻烦事,不过官方已经给出一些rc.d脚本示例了,在github上,不过由于我是apt-get安装的,显然这个开机自启动是不用担心的,用debian的update-rc.d即可搞定。

Supervisor的进程安全

        既然是用Supervisor来保证其他进程的正常运行,但是万一Supervisor进程挂了怎么办,我们可以使用daemontools来保证Supervisor正常运行,就类似于监控的监控。

supervisord的配置文件主要由几个配置段构成,配置项以K/V格式呈现,下面就看看各个配置端需要怎样配置:

[unix_http_server]

    在该配置块的参数项表示的是一个监听在socket上的HTTP server,如果[unix_http_server]块不在配置文件中,则不会启动基于socket的HTTP server。

file:一个unix domain socket的文件路径,HTTP/XML-RPC会监听在这上面
chmod:在启动时修改unix domain socket的mode
chown:修改socket文件的属主
username:HTTP server在认证时的用户名
password:认证密码<span style="font-family:'Microsoft YaHei';font-size:16px;line-height:1.5;"></span>
eg:

复制代码代码如下:
[unix_http_server]
file = /tmp/supervisor.sock
chmod = 0777
chown= nobody:nogroup
username = user
password = 123
[inet_http_server]

    在该配置块的参数项表示的是一个监听在TCP上的HTTP server,如果[inet_http_server]块不在配置文件中,则不会启动基于TCP的HTTP server。

port:TCP监听的地址和端口(ip:port),这个地址会被HTTP/XML-RPC监听
username:HTTP server在认证时的用户名
password:认证密码
eg:

复制代码代码如下:
[inet_http_server]
port = 127.0.0.1:9001
username = user
password = 123
[supervisord]

该配置块的参数项是关于supervisord进程的全局配置项。
logfile:log文件路径
logfile_maxbytes:log文件达到多少后自动进行轮转,单位是KB、MB、GB。如果设置为0则表示不限制日志文件大小
logfile_backups:轮转日志备份的数量,默认是10,如果设置为0,则不备份
loglevel:error、warn、info、debug、trace、blather、critical
pidfile:pid文件路径
umask:umask值,默认022
nodaemon:如果设置为true,则supervisord在前台启动,而不是以守护进程启动
minfds:supervisord在成功启动前可用的最小文件描述符数量,默认1024
minprocs:supervisord在成功启动前可用的最小进程描述符数量,默认200
nocleanup:防止supervisord在启动的时候清除已经存在的子进程日志文件
childlogdir:自动启动的子进程的日志目录
user:supervisord的运行用户
directory:supervisord以守护进程运行的时候切换到这个目录
strip_ansi:消除子进程日志文件中的转义序列
environment:一个k/v对的list列表
eg:

复制代码代码如下:
[supervisord]
logfile = /tmp/supervisord.log
logfile_maxbytes = 50MB
logfile_backups=10
loglevel = info
pidfile = /tmp/supervisord.pid
nodaemon = false
minfds = 1024
minprocs = 200
umask = 022
user = chrism
identifier = supervisor
directory = /tmp
nocleanup = true
childlogdir = /tmp
strip_ansi = false
environment = KEY1="value1",KEY2="value2"
[supervisorctl]

    该配置块参数是关于supervisorctl

serverurl:这个url是用来访问supervisord服务的(http://localhost:9001),或者是个sockets文件(unix:///absolute/path/to/file.sock)
username:supervisorctl连接supervisord的认证用户
password:认证密码
prompt:默认是supervisor
history_file:history文件路径
eg:

复制代码代码如下:
[supervisorctl]
serverurl = unix:///tmp/supervisor.sock
username = chris
password = 123
prompt = mysupervisor
[program:x]

    该配置块包含一个或者多个program段,program来表明supervisord要控制哪些程序。该配置块的头部是有固定格式的,一个关键字program,后面跟着一个冒号,接下来才是程序名。例如:[program:foo],foo就是程序名,在使用supervisorctl来操作程序的时候,就是以foo来标明的。

command:启动程序使用的命令,可以是绝对路径或者相对路径
process_name:一个python字符串表达式,用来表示supervisor进程启动的这个的名称,默认值是%(program_name)s
numprocs:Supervisor启动这个程序的多个实例,如果numprocs>1,则process_name的表达式必须包含%(process_num)s,默认是1
numprocs_start:一个int偏移值,当启动实例的时候用来计算numprocs的值
priority:权重,可以控制程序启动和关闭时的顺序,权重越低:越早启动,越晚关闭。默认值是999
autostart:如果设置为true,当supervisord启动的时候,进程会自动重启。
autorestart:值可以是false、true、unexpected。false:进程不会自动重启,unexpected:当程序退出时的退出码不是exitcodes中定义的时,进程会重启,true:进程会无条件重启当退出的时候。
startsecs:程序启动后等待多长时间后才认为程序启动成功
startretries:supervisord尝试启动一个程序时尝试的次数。默认是3
exitcodes:一个预期的退出返回码,默认是0,2。
stopsignal:当收到stop请求的时候,发送信号给程序,默认是TERM信号,也可以是 HUP, INT, QUIT, KILL, USR1, or USR2。
stopwaitsecs:在操作系统给supervisord发送SIGCHILD信号时等待的时间
stopasgroup:如果设置为true,则会使supervisor发送停止信号到整个进程组
killasgroup:如果设置为true,则在给程序发送SIGKILL信号的时候,会发送到整个进程组,它的子进程也会受到影响。
user:如果supervisord以root运行,则会使用这个设置用户启动子程序
redirect_stderr:如果设置为true,进程则会把标准错误输出到supervisord后台的标准输出文件描述符。
stdout_logfile:把进程的标准输出写入文件中,如果stdout_logfile没有设置或者设置为AUTO,则supervisor会自动选择一个文件位置。
stdout_logfile_maxbytes:标准输出log文件达到多少后自动进行轮转,单位是KB、MB、GB。如果设置为0则表示不限制日志文件大小
stdout_logfile_backups:标准输出日志轮转备份的数量,默认是10,如果设置为0,则不备份
stdout_capture_maxbytes:当进程处于stderr capture mode模式的时候,写入FIFO队列的最大bytes值,单位可以是KB、MB、GB
stdout_events_enabled:如果设置为true,当进程在写它的stderr到文件描述符的时候,PROCESS_LOG_STDERR事件会被触发
stderr_logfile:把进程的错误日志输出一个文件中,除非redirect_stderr参数被设置为true
stderr_logfile_maxbytes:错误log文件达到多少后自动进行轮转,单位是KB、MB、GB。如果设置为0则表示不限制日志文件大小
stderr_logfile_backups:错误日志轮转备份的数量,默认是10,如果设置为0,则不备份
stderr_capture_maxbytes:当进程处于stderr capture mode模式的时候,写入FIFO队列的最大bytes值,单位可以是KB、MB、GB
stderr_events_enabled:如果设置为true,当进程在写它的stderr到文件描述符的时候,PROCESS_LOG_STDERR事件会被触发
environment:一个k/v对的list列表
directory:supervisord在生成子进程的时候会切换到该目录
umask:设置进程的umask
serverurl:是否允许子进程和内部的HTTP服务通讯,如果设置为AUTO,supervisor会自动的构造一个url
   
eg:

复制代码代码如下:
[program:cat]
command=/bin/cat
process_name=%(program_name)s
numprocs=1
directory=/tmp
umask=022
priority=999
autostart=true
autorestart=true
startsecs=10
startretries=3
exitcodes=0,2
stopsignal=TERM
stopwaitsecs=10
user=chrism
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


  • 上一条:
    Linux系统下OpenSSH的安装及基本配置文件详解
    下一条:
    Linux系统中的sudo命令使用讲解
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在Linux系统中使用Iptables实现流量转发功能流程步骤(0个评论)
    • vim学习笔记-入门级需要了解的一些快捷键(0个评论)
    • 在centos7系统中实现分区并格式化挂载一块硬盘到/data目录流程步骤(0个评论)
    • 在Linux系统种查看某一个进程所占用的内存命令(0个评论)
    • Linux中grep命令中的10种高级用法浅析(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-11
    • 2017-07
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-03
    • 2022-04
    • 2022-08
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-06
    • 2023-07
    • 2023-10
    • 2023-12
    • 2024-01
    • 2024-04
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客