• Supervisor-类unix系统下的进程控制工具


    如果你的英文足够好,请看官网的文档:http://supervisord.org/introduction.html

    简介:

    Supervisor 类unix系统下的进程控制工具。

    特性:

    1.配置简单

    2.统一管理

    3.高效

    当supervisor为用户启动的子进程中止时,操作系统会发出信号告诉supervisor。(而不是轮询进程id来判断的)

    4.可扩展

    supervisor有一个简单的事件通知协议,可以通过任何语言去监视它。

    5.兼容性

    除了windows以外都兼容。开发语言是python,不用安装c编译器。

    6.久经考验

    Supervisor包含的组件:

    supervisord

    服务端的组件,主要功能开启子进程,相应客户端的命令,重启一些有问题的子进程,记录日志,生成与处理一些事件。

    这个服务进程使用了一个配置文件,通常在 /etc/supervisord.conf 这个位置。这个配置文件是Windows-INI风格的配置文件。

    通过正确的文件系统权限保持这个文件的安全是十分必要的,因为它可能包含一些明文的用户名密码。

    supervisorctl

    客户端组件,类shell界面。用户能连接上不同的supervisor进程得到子进程的状态,然后控制状态,等等。

    Web Server

    通过web界面查看当前的进程状态。http://localhost:9001/

    XML-RPC Interface

    具体请看http://supervisord.org/api.html#xml-rpc

    平台需求:

    Linux (Ubuntu 9.10)

    Mac OS X (10.4/10.5/10.6)

    Solaris (10 for Intel)

    FreeBSD 6.1

    Python>=2.4 但不包含3

    安装

    sudo apt-get install supervisor

    运行Supervisor

    添加一个程序

    在supervisord可用之前,你将至少添加一个[program]部分。当你用supervisord命令运行和管理一个程序的时候,就是通过[program]来定义的。想要添加一个程序,你需要编辑supervisord.conf文件。

    一个最简单的运行unix cat的程序。当supervisord进程启动的时候,这个[program]部分将会运行cat命令。

    [program:foo]
    command=/bin/cat

    这段代码可以被剪切然后粘贴到supervosord.conf文件中。这是一个最简单的程序配置,因为它只有一个命令部分。程序配置部分可以有很多其他的配置选项,没有展示出来。具体请看:

    http://supervisord.org/configuration.html#programx-section

    运行supervisord

    想要开始supervisord,运行$BINDIR/supervisord。进程将会后台运行不会阻塞终端,默认在$CWD/supervisor.log目录下有操作日志。

    你也可以开始使用-n参数前台运行supervisord。这对于debug来说很有用。

    警告:

    当supervisord启动的时候,它将搜索它的配置文件在默认的位置(当前的工作目录)。如果你出于安全方面考虑你可以使用-c参数指定配置文件的绝对路径(别人就不能冒充你运行了)。

    想要通过supervisord改变程序的控制,编辑supervisord.conf文件然后运行kill -HUP或者重启supervisord进程。这个文件有很多例子程序定义。

    supervisord命令接受很多选项。

    supervisord命令行选项

     
    -c FILE, --configuration=FILE
     

    The path to a supervisord configuration file.

    这个目录是一个supervisord配置文件。

    -n, --nodaemon

    Run supervisord in the foreground.

    前台运行supervisord

    -h, --help

    Show supervisord command help.

    展示命令帮助

    -u USER, --user=USER
      UNIX username or numeric user id. If supervisord is started as the root user, setuid to this user as soon as possible during startup.
    -m OCTAL, --umask=OCTAL
      Octal number (e.g. 022) representing the umask that should be used by supervisord after it starts.
    -d PATH, --directory=PATH
     

    When supervisord is run as a daemon, cd to this directory before daemonizing.

    当supervisord作为守护进程运行之前,会进入到这个目录。

    -l FILE, --logfile=FILE
     

    Filename path to use as the supervisord activity log.

    活动日志路径。

    -y BYTES, --logfile_maxbytes=BYTES
     

    Max size of the supervisord activity log file before a rotation occurs. The value is suffix-multiplied, e.g “1” is one byte, “1MB” is 1 megabyte, “1GB” is 1 gigabyte.

    日志文件最大大小,

    -y NUM, --logfile_backups=NUM
     

    Number of backup copies of the supervisord activity log to keep around. Each logfile will be of size logfile_maxbytes.

    保留日志副本数。

    -e LEVEL, --loglevel=LEVEL
     

    The logging level at which supervisor should write to the activity log. Valid levels are trace, debug, info, warn, error, and critical.

    设置日志等级。

    -j FILE, --pidfile=FILE
      The filename to which supervisord should write its pid file.
    -i STRING, --identifier=STRING
      Arbitrary string identifier exposed by various client UIs for this instance of supervisor.
    -q PATH, --childlogdir=PATH
     

    A path to a directory (it must already exist) where supervisor will write its AUTO -mode child process logs.

    子进程日志

    -k, --nocleanup
      Prevent supervisord from performing cleanup (removal of old AUTO process log files) at startup.
    -a NUM, --minfds=NUM
     

    The minimum number of file descriptors that must be available to the supervisord process before it will start successfully.

    文件描述器的最小个数。

    -t, --strip_ansi
      Strip ANSI escape sequences from all child log process.
    -v, --version

    Print the supervisord version number out to stdout and exit.

    版本号

    --profile_options=LIST
      Comma-separated options list for profiling. Causes supervisord to run under a profiler, and output results based on the options, which is a comma-separated list of the following: cumulative, calls, callers. E.g. cumulative,callers.
    --minprocs=NUM The minimum number of OS process slots that must be available to the supervisord process before it will start successfully.

     运行supervisorctl

    想要开始supervisorctl,运行$BINDIR/supervisorctl。这个 shell就可以让你控制进程(被托管在supervisord中的),键入help可以得到支持的命令信息。

    如果是交互模式有可能需要授权。

    Supervisorctl命令行选项

    -c--configuration
     

    Configuration file path (default /etc/supervisord.conf)

    配置文件路径

    -h--help

    Print usage message and exit

    帮助

    -i--interactive
     

    Start an interactive shell after executing commands

    开始交互shell

    -s--serverurl URL
     

    URL on which supervisord server is listening (default “http://localhost:9001”).

    指定supervisord服务端地址。

    -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)

    保持历史。

    格式:行为 [arguments],下面的表格就是命令中的行为

    help

    Print a list of available actions
    列出可用的行为

    help <action>

    Print help for <action>
    查看某个行为的帮助

    add <name> [...]

    Activates any updates in config for process/group
    激活配置中的进程/组

    remove <name> [...]

    Removes process/group from active config
    从配置中删除进程/组

    update

    Reload config and then add and remove as necessary (restarts programs)
    重载程序

    clear <name>

    Clear a process’ log files.
    清除进程日志文件

    clear <name> <name>

    Clear multiple process’ log files
    清楚多个进程日志文件

    clear all

    Clear all process’ log files
    清理所有进程日志文件

    fg <process>

    Connect to a process in foreground mode Press Ctrl+C to exit foreground
    用前台模式链接一个进程,按ctrl+c推出前台。

    pid

    Get the PID of supervisord.
    得到supervisord的进程id

    pid <name>

    Get the PID of a single child process by name.
    通过名字得到进程id

    pid all

    Get the PID of every child process, one per line.
    得到每一个子进程的进程id,一个一行。

    reread

    Reload the daemon’s configuration files, without add/remove (no restarts)
    重载进程守护配置文件。而不add/remove,不是重启。

    restart <name>

    Restart a process Note: restart does not reread config files. For that, see reread and update.
    重启一个进程,但不读配置文件。

    restart <gname>:*

    Restart all processes in a group Note: restart does not reread config files. For that, see reread and update.
    重启一个组内的所有进程,不读配置文件。

    restart <name> <name>

    Restart multiple processes or groups Note: restart does not reread config files. For that, see reread and update.
    重启多个进程/组。不读配置文件

    restart all

    Restart all processes Note: restart does not reread config files. For that, see reread and update.
    重启所有进程,

    signal

    No help on signal

    start <name>

    Start a process
    开始一个进程

    start <gname>:*

    Start all processes in a group
    开始一个组内所有进程

    start <name> <name>

    Start multiple processes or groups
    开始多个进程/组

    start all

    Start all processes
    开启所有进程

    status

    Get all process status info.
    得到所有进程状态信息

    status <name>

    Get status on a single process by name.
    通过名字得到一个进程的状态

    status <name> <name>

    Get status on multiple named processes.
    通过多个进程名字查看状态

    stop <name>

    Stop a process
    停止一个进程。

    stop <gname>:*

    Stop all processes in a group
    停止一个组内的所有进程

    stop <name> <name>

    Stop multiple processes or groups
    停止多个进程/组

    stop all

    Stop all processes
    停止所有进程。

    tail [-f] <name> [stdout|stderr] (default stdout)

    Output the last part of process logs Ex: tail -f <name> Continuous tail of named process stdout Ctrl-C to exit. tail -100 <name> last 100 bytes of process stdout tail <name> stderr last 1600 bytes of process stderr
    输出进程日志。

    信号

    supervisord程序可能会发一些信号,让其在运行时执行执行某些操作。

    你可能发送任意一个信号到单个supervisord进程id中。这个进程id能够被发现在配置文件的[supervisord]部分的pidfile参数上(配置文件默认在$CWD/supervisord.pid这个位置)。

    信号处理器

    SIGTERM    supervisord和它的子进程将会关闭。这可能会花费几秒。

    SIGINT   supervisord和它的子进程将会关闭。这可能会花费几秒。

    SIGQUIT    supervisord和它的子进程将会关闭。这可能会花费几秒。

    SIGHUP     supervisord和它的子进程将会关闭,从找到的第一个配置文件重载配置,开启所有进程,

    SIGUSR2    supervisord将会关闭然后重新打开主活动日志和所有子日志文件。

    运行时安全

    配置文件

    supervisor配置文件通常叫supervisord.conf,它被supervisord和supervisorctl使用。如果应用运行没有使用-c参数(这个参数精确的告诉配置文件的位置),这个应用将会在以下位置按照指定的准许寻找supervisord.conf文件,然后只用第一个被找到的文件。

    1. $CWD/supervisord.conf
    2. $CWD/etc/supervisord.conf
    3. /etc/supervisord.conf
    4. /etc/supervisor/supervisord.conf (3.3.0版本以后)
    5. ../etc/supervisord.conf (相对于可执行文件)
    6. ../supervisord.conf (相对于可执行文件)

    文件格式

    supervisord.conf是一个windiws ini格式分割的文件,它有[selction]部分,键值对在这个section内,sections和它允许的值被允许在下面:

    环境变量

    可以使用python字符串表达式语法%(ENV_X)s,

    [program:example]
    command=/usr/bin/example --loglevel=%(ENV_LOGLEVEL)s

     在上面的例子中,表达式%(ENV_LOGLEVEL)s将会被环境变量LOGLEVEL的值。

    注意:

    在Supervisor3.2和之后,表达式%(ENV_X)s支持所有选项,之前的版本中大多数选项不支持。看文档下面的每个选项:

    太多了。请参考

    http://supervisord.org/configuration.html

    子进程

    supervisord的主要目的是基于配置文件的数据创建和管理进程,每一个被supervisor创建的子进程被supervisord管理着整个的生命周期。supervisod是这些子进程的父进程,当子进程死亡后,supervisor被SIGCHLD信号通知,并执行适当的操作。

     非进程守护的子进程

    运行在supervisor下的程序应该不能自我守护进程,相反,它们应该给运行在前台,它们不应该从它们开始运行时的终端脱离。

    判断程序是否在前台的最简单的方法就是运行shell提示符调用程序的命令。如果它返回了一个终端返回的控制状态,但扔继续运行,这就是自我进程守护了,这也就意味着这是一个在supervisor下一个错误的方式。如果你想运行一个命令,你需要按ctrl+c让终端返回控制,如果它在运行之后没有按ctrl+c就返回了一个shell提示符,这就说明它在supervisor是没有用的。所有的在前台的程序都有选项,但是却没有一个标准的方式,你需要读它们每个程序各自的文档。

    下面是配置文件的例子,这是一个通用的运行在supervisor下的前台模式程序。

    程序配置的例子

    Apache 2.2.6

    [program:apache2]
    command=/path/to/httpd -c "ErrorLog /dev/stdout" -DFOREGROUND
    redirect_stderr=true

    Two Zope 2.X instances and one ZEO server

    [program:zeo]
    command=/path/to/runzeo
    priority=1
    
    [program:zope1]
    command=/path/to/instance/home/bin/runzope
    priority=2
    redirect_stderr=true
    
    [program:zope2]
    command=/path/to/another/instance/home/bin/runzope
    priority=2
    redirect_stderr=true

    Postgres 8.X

    [program:postgres]
    command=/path/to/postmaster
    ; we use the "fast" shutdown signal SIGINT
    stopsignal=INT
    redirect_stderr=true

    OpenLDAP slapd

    [program:slapd]
    command=/path/to/slapd -f /path/to/slapd.conf -h ldap://0.0.0.0:8888
    redirect_stderr=true

     其他程序的脚本例子能够在http://thedjbway.b0llix.net/services.html这里找到,这些例子实际上是为了daemontool准备的,但对supervisor也一样。

     pidproxy 进程id代理程序

    一些进程(像mysql)忽视supervisord进程发出的信号,相反,一些“特殊”的进程或线程被这些专门处理信号的程序创建。这是有问题的,因为supervisord仅能够杀死它自己创建的进程,而supervisord不能杀死它们。

    幸运的是,这些典型类型的程序写了一个“pidfile”文件,这个文件包含了这些创建的“特殊”进程id,这意味着能够读这个文件,然后使用它去杀死进程。作为这种情况的解决办法,一个特殊的pidproxy程序能够处理这些程序的运行。 这个pid代理程序是一个小垫片,启动一个程序后,接收一些进程id的信号,写入的pidfile文件中。下面是一个示范的配置:

    [program:mysql]
    command=/path/to/pidproxy /path/to/pidfile /path/to/mysqld_safe

     子进程 环境

     子进程集成了supervisord程序启动时的shell环境,许多环境变量将会被supervisord设置。包括SUPERVISOR_ENABLED(它是一个标记说明这个进程在supervisor的控制之下),SUPERVISOR_PROCESS_NAME(指定了进程的名字)、SUPERVISOR_GROUP_NAME(指定了子进程的组名)。

    这些环境变量可能会被重写在[supervisord]部分的environment选项(应用到所有的子进程),或者在每一个[program:x]内的environment配置选项(仅仅应用到每个指定的子进程中)。这些environment是附件的。换句话说,每个子进程的环境将会包括:

    shell中设置的

    ... added-to/overridden-by ...

    全局environment中设置的

    config option ...

    ... added-to/overridden-by ...

    ... supervisor-specific environment variables
    (SUPERVISOR_ENABLEDSUPERVISOR_PROCESS_NAME,SUPERVISOR_GROUP_NAME) ..

    ... added-to/overridden-by ...

    指定子进程 environment中设置的

    当通过supervisord运行一个子进程的时候没有shell被执行,所以环境像USER、PATH、HOME、SHELL、LOGNAME等等,不会从其默认值改变。

    http://supervisord.org/introduction.html
  • 相关阅读:
    第一个Polymer应用
    Android-注解处理器
    怎样启动JDBC Debug模式,打印JDBC诊断日志
    OA项目之权限设计②
    Redis安装教程
    Skyscrapers Aren’t Scalable
    codeforces 183B
    iOS-NSMutableParagraphStyle段落风格
    【Java集合源代码剖析】Java集合框架
    经常使用的Hql语句
  • 原文地址:https://www.cnblogs.com/handsomer/p/8284860.html
Copyright © 2020-2023  润新知