用Supervisord管理进程

Supervisord是用Python实现的一款非常实用的进程管理工具,类似于monit。

Monit和Supervisord的一个比较大的差异是Supervisord管理的进程必须由Supervisord来启动,Monit可以管理已经在运行的程序。

Supervisord还要求管理的程序是非Daemon程序,Supervisord会帮你把它转成Daemon程序,因此如果用Supervisord来管理Nginx的话,必须在Nginx的配置文件里添加一行设置Daemon off让Nginx以非Daemon方式启动。

一、Supervisord安装

Supervisord可以通过easy_install supervisor安装,当然也可以通过Supervisord官网下载后setup.py install安装。也可以通过其他包管理来安装,比如apt/yum等。

我这里用easy_install supervisor来安装:

# yum install python-setuptools

# easy_install supervisor

安装好以后,有两个可执行文件和一个脚本文件

/usr/bin/supervisord      
      --  supervisor服务守护进程

/usr/bin/supervisorctl           --  supervisor服务控制程序,比如:status/start/stop/restart
xx 等

/usr/bin/echo_supervisord_conf   --  supervisor产生配置文件样例

# echo_supervisord_conf > /etc/supervisord.conf  -- 生成配置文件

二、Supervisor配置

#vi /etc/supervisord.conf

[unix_http_server]          ; supervisord的unix socket服务配置
file=/tmp/supervisor.sock   ; socket文件的保存目录
;chmod=0700                 ; socket的文件权限 (default 0700)
;chown=nobody:nogroup       ; socket的拥有者和组名
;username=user              ; 默认不需要登陆用户 (open server)
;password=123               ; 默认不需要登陆密码 (open server)

[inet_http_server]         ; supervisord的tcp服务配置
; Web管理界面设定
port=127.0.0.1:9001        ; tcp端口
username=user              ; tcp登陆用户
password=123               ; tcp登陆密码

[supervisord]                ; supervisord的主进程配置
logfile=/tmp/supervisord.log ; 主要的进程日志配置
logfile_maxbytes=50MB        ; 最大日志体积,默认50MB
logfile_backups=10           ; 日志文件备份数目,默认10
loglevel=info                ; 日志级别,默认info; 还有:debug,warn,trace
pidfile=/tmp/supervisord.pid ; supervisord的pidfile文件
nodaemon=false               ; 是否以守护进程的方式启动
minfds=1024                  ; 最小的有效文件描述符,默认1024
minprocs=200                 ; 最小的有效进程描述符,默认200
;umask=022                   ; 进程文件的umask,默认200
;user=chrism                 ; 默认为当前用户,如果为root则必填
;identifier=supervisor       ; supervisord的表示符, 默认时'supervisor'
;directory=/tmp              ; 默认不cd到当前目录
;nocleanup=true              ; 不在启动的时候清除临时文件,默认false
;childlogdir=/tmp            ; ('AUTO' child log dir, default $TEMP)
;environment=KEY=value       ; 初始键值对传递给进程
;strip_ansi=false            ; (strip ansi escape codes in logs; def. false)

[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket
;username=chris              ; 如果设置应该与http_username相同
;password=123                ; 如果设置应该与http_password相同
;prompt=mysupervisor         ; 命令行提示符,默认"supervisor"
;history_file=~/.sc_history  ; 命令行历史纪录

; 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

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

;[eventlistener:theeventlistenername]
;command=/bin/eventlistener    ; 运行的程序 (相对使用PATH路径, 可以使用参数)
;process_name=%(program_name)s ; 进程名表达式,默认为%(program_name)s
;numprocs=1                    ; 默认启动的进程数目,默认为1
;events=EVENT                  ; event notif. types to subscribe to (req'd)
;buffer_size=10                ; 事件缓冲区队列大小,默认10
;directory=/tmp                ; 在运行前cwd到指定的目录,默认不执行cmd
;umask=022                     ; 进程umask,默认None
;priority=-1                   ; 程序运行的优先级,默认-1
;autostart=true                ; 默认随supervisord自动启动,默认true
;autorestart=unexpected        ; whether/when to restart (default: unexpected)
;startsecs=1                   ; number of secs prog must stay running (def. 1)
;startretries=3                ; max # of serial start failures (default 3)
;exitcodes=0,2                 ; 期望的退出码,默认0,2
;stopsignal=QUIT               ; 杀死进程的信号,默认TERM
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false             ; 向unix进程组发送停止信号,默认false
;killasgroup=false             ; 向unix进程组发送SIGKILL信号,默认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 (default 10)
;stdout_events_enabled=false   ; emit events on stdout writes (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        ; # of stderr logfile backups (default 10)
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;environment=A=1,B=2           ; process environment additions
;serverurl=AUTO                ; override serverurl computation (childutils)

[group:appgroup]
programs=wapp,wfapp           ; 任何在[program:x]中定义的x
;priority=999                  ; 程序运行的优先级,默认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 = relative/directory/*.ini

; 管理的单个进程的配置,可以添加多个program
[program:cobar]
; 被监控程序指定的运行脚本
; 如果command的栏运行的是shell脚本,那么在shell 脚本启动被监控程序时要用exec修饰。例如:
;   echo $RUN_CMD
;   eval exec $RUN_CMD
; 否则,supervisord停止不了启动的进程.除此之外,shell脚本里不能出现&之类的后台运行符号
command=/bin/sh /home/jfy/soft/cobar-server-1.2.7/bin/startup2.sh
;process_name=%(program_name)s ; 进程名表达式,默认为%(program_name)s
;numprocs=1                    ; 默认启动的进程数目,默认为1
directory=/home/jfy/soft/cobar-server-1.2.7/bin                    ; 在运行前cwd到指定的目录,默认不执行cmd
;umask=022                     ; 进程umask,默认None

priority=1                    ; 程序运行的优先级,默认999
autostart=true                ; 默认随supervisord自动启动,默认true
autorestart=true              ; 被监控程序异常中断是否自动重启
startsecs=5                   ; 被监控程序启动时持续时间
startretries=5                ; 被监控程序启动失败重试的次数
;exitcodes=0,2                 ; 期望的退出码,默认0,2
;stopsignal=QUIT               ; 杀死进程的信号,默认TERM
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false             ; 向unix进程组发送停止信号,默认false
;killasgroup=false             ; 向unix进程组发送SIGKILL信号,默认false
;user=root                     ; 为运行程序的unix帐号设置setuid
redirect_stderr=true          ; 将标准错误重定向到标准输出,默认false
stdout_logfile=/home/jfy/soft/cobar-server-1.2.7/logs/console.log        ; stdou 重定向输出文件
;stdout_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10     ; # of stdout logfile backups (default 10)
;stdout_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
;stdout_events_enabled=false   ; emit events on stdout writes (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 (default 10)
;stderr_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;environment=A=1,B=2           ; process environment additions (def no adds)
;serverurl=AUTO                ; override serverurl computation (childutils)

[program:cardapi]
command=/usr1/app/run/CARDAPI /usr1/app/ini/cardapi.ini /usr1/app/log/cardapi.tr
priority=1
numprocs=1
autostart=true
autorestart=true

; 配置一组进程,对于类似的program可以通过这种方式添加,避免手工一个个添加
;[program:bsapp]
;command=/usr1/app/run/%(program_name)s_%(process_num)02d wapp /usr1/app/log/%(program_name)s_%(process_num)02d.tr
;numprocs=3
;process_name=%(program_name)s_%(process_num)02d
;autostart=true
;autorestart=true</span><span style="font-family: 微软雅黑, 'Microsoft YaHei', Oswald, 'Helvetica Neue', Helvetica, Verdana, arial, sans-serif;">

(更多配置说明请参考:http://supervisord.org/configuration.html)

三、Supervisor管理

Supervisord安装完成后有两个可用的命令行supervisor和supervisorctl。

启动Supervisord

# supervisord

启动时指定配置文件

# supervisord -c /etc/supervisord.conf

# supervisord --help
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')

supervisorctl命令行控制程序

# supervisorctl
appgroup:wapp                    RUNNING   pid 42981, uptime 0:01:45
appgroup:wfapp                   RUNNING   pid 42982, uptime 0:01:45
cardapi                          RUNNING   pid 33133, uptime 1:02:32
cobar                            RUNNING   pid 6761, uptime 22:43:42
gearmand                         RUNNING   pid 6743, uptime 22:43:53
memcached                        RUNNING   pid 6728, uptime 22:44:05
supervisor> help
default commands (type help <topic>):
=====================================
add    clear  fg        open  quit    remove  restart   start   stop  update
avail  exit   maintail  pid   reload  reread  shutdown  status  tail  version

stop <name>      停止某一个进程(name),name为[program:cobar]里配置的值,这个示例就是cobar。
start <name>     启动某个进程
restart <name>   重启某个进程
stop <gname>:*   重启属于名为gname这个分组的进程(start,restart同理) ,如:stop appgroup:wapp
stop all         停止全部进程,注:start、restart、stop都不会载入最新的配置文件。
reload           载入最新的配置文件,停止原有进程并按新的配置启动、管理所有进程。
update           根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启。 注意:显示用stop停止掉的进程,用reload或者update都不会自动重启。

OK,基本的操作就是类似这个了

supervisor还支持以WEB方式监控管理进程

找到[inet_http_server]这一段,修改成

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

其中port这个字段要各位注意,如果*:9001表示允许所有ip访问,如果指定单个IP可以 xx.xx.xx.xx:9001 这样既可。如果你开启了iptabls记得要在规则里允许port指定的端口号。

然后保存配置,重启supervisord

# supervisord reload

启用浏览器访问效果:

时间: 2024-08-10 21:18:35

用Supervisord管理进程的相关文章

supervisord管理进程详解

Supervisor是由python语言编写,基于linux操作系统的一款服务器管理工具, 用以监控服务器的运行,发现问题能立即自动预警及自动重启等功能. Supervisor类似于monit, monit和supervisor的一个比较大的差异是supervisor管理的进程必须由supervisor来启动, monit可以管理已经在运行的程序: supervisor还要求管理的程序是非daemon程序,supervisord会帮你把它转成daemon程序, 因此如果用supervisor来管

用Supervisord管理Python进程

http://feilong.me/2011/03/monitor-processes-with-supervisord Supervisord是用Python实现的一款非常实用的进程管理工具,类似于monit(关于monit见我的博客:用monit监控系统关键进程),monit和supervisord的一个比较大的差异是supervisord管理的进程必须由supervisord来启动,monit可以管理已经在运行的程序:supervisord还要求管理的程序是非daemon程序,superv

使用Supervisor管理进程二

supervisor安装完成后会生成三个执行程序:supervisortd.supervisorctl.echo_supervisord_conf,分别是supervisor的守护进程服务(用于接收进程管理命令).客户端(用于和守护进程通信,发送管理进程的指令).生成初始配置文件程序. 3.配置 运行supervisord服务的时候,需要指定supervisor配置文件,如果没有显示指定,默认在以下目录查找: $CWD/supervisord.conf$CWD/etc/supervisord.c

使用supervisor管理进程

Supervisor (http://supervisord.org) 是一个用 Python 写的进程管理工具,可以很方便的用来启动.重启.关闭进程(不仅仅是 Python 进程).除了对单个进程的控制,还可以同时启动.关闭多个进程,比如很不幸的服务器出问题导致所有应用程序都被杀死,此时可以用 supervisor 同时启动所有应用程序而不是一个一个地敲命令启动. 安装 Supervisor 可以运行在 Linux.Mac OS X 上.如前所述,supervisor 是 Python 编写的

docker技术剖析--docker supervisor管理进程

http://hongge.blog.51cto.com/ 使用 Supervisor 来管理进程 Docker 容器在启动的时候开启单个进程,比如,一个 ssh 或者 apache 的 daemon 服务.但我们经常需要在一个机器上开启多个服务,这可以有很多方法,最简单的就是把多个启动命令放到一个启动脚本里面,启动的时候直接启动这个脚本. 例如:docker run –d 镜像 /run.sh 另外就是安装进程管理工具. 本节将使用进程管理工具 supervisor 来管理容器中的多个进程.使

使用 Supervisor 管理进程

1.介绍 Supervisor是一个客户端/服务器系统,允许用户在类UNIX操作系统上控制大量进程. 作用:为每个实例编写启动脚本通常是不方便的. 编写和维护会很痛苦.此外,脚本不能自动重启崩溃的进程,并且很多程序在崩溃时不能自行正常重启.Supervisord作为其子进程启动进程,并且可以配置为在崩溃时自动重启进程.它也可以自动配置为在自己的调用中启动进程. 2.安装 Supervisor yum install supervisor -y 更多安装方法详见官方手册:http://www.su

进程管理—进程描述符(task_struct)

http://blog.csdn.net/qq_26768741/article/details/54348586 当把一个程序加载到内存当中,此时,这个时候就有了进程,关于进程,有一个相关的叫做进程控制块(PCB),这个是系统为了方便进行管理进程所设置的一个数据结构,通过PCB,就可以记录进程的特征以及一些信息. 内核当中使用进程描述符task_struct. 这个task_struct就是一个定义的一个结构体,通过这个结构体,可以对进程的所有的相关的信息进行维护,对进程进行管理. 接下来我们

管理进程及设置计划任务

实验案列:管理进程及设置计划任务 需求:管理系统中进程 设置计划运行的系统管理任务 步骤: 1管理系统中地进程 启动系统中portmap服务,确认服务运行状态,通过ps或pgrep命令查看portmap的进程信息 Ps:查看静态的进程统计信息,a:显示当前终端下的所有进程信息,u:使用以用户为主的格式输出进程信息,x: 显示当前用户在所有终端下的进程信息,-e:显示系统内的所有进程信息,-l:使用长格式显示进程信息,-f:使用完整的格式显示进程信息 Ps –elf:以长格式显示系统中所有的进程信

转】Nginx系列(三)--管理进程、多工作进程设计

原博文出于:http://blog.csdn.net/liutengteng130/article/details/46700999  感谢! Nginx由一个master进程和多个worker进程组成,但master进程或者worker进程中并不会再创建线程. 一.master进程和worker进程的作用 master进程 不需要处理网络事件,不负责业务的执行,只会通过管理worker等子进程来实现重启服务.平滑升级.更换日志文件.配置文件实时生效等功能. master是通过fork系统调用子