Linux系统下的进程监控工具Supervisor的基本使用方法_unix linux教程-查字典教程网
Linux系统下的进程监控工具Supervisor的基本使用方法
Linux系统下的进程监控工具Supervisor的基本使用方法
发布时间:2016-12-27 来源:查字典编辑
摘要:Supervisor是一个C/S系统,它允许用户在类UNIX系统上控制一些进程。它具有以下特性:1简单Supervisor通过INI格式配置...

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

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新unix linux学习
    热门unix linux学习
    操作系统子分类