Supervisor
Supervisor is a client/server system that allows its users to control a number of processes on
UNIX-like operating systems.
注意:
Supervisor has been tested and is known to run on Linux (Ubuntu 9.10), Mac OS X (10.4/10.5/10.6),
and Solaris (10 for Intel) and FreeBSD 6.1. It will likely work fine on most UNIX systems. Supervisor will not run at all under any version of Windows. Supervisor is known to work with Python 2.4 or later but will not work under any version of
Python 3.
线上环境服务停止: 1、手动重启 2、集群、负载均衡 3、supervisor 前提: 没有集群、负载均衡, 手动启动时间又无法接收 优点: 出现问题,可以马上拉起程序 启动自动化,减少人为因素的干预 缺点: 程序本身有BUG,它挂掉了,但又通过supervisor起来了(若果它对系统产生影响,例如open-files,它不断打开文件,达到系统上限,--》系统崩溃, 有一个影响,加剧影响)
特点:
- 简单
- 高效
- 集中
- 可扩展
组成:
- supervisord:服务器部分(监督者)
功能:在自己的调用中启动子程序,响应客户机的命令,重新启动崩溃或推出子进程,记录其子进程stdout和stderr输出,
生成和处理子进程生命周期中对应的‘事件’
- supervisorctl:客户机部分
提供了一个类似于shell的接口,可以连接到监视器提供的特性。客户可以从weaksorctl连接到不同的监控程序(一次一个),
获得被监控子进程的状态,停止并重新启动子进程,并获得监控进程的运行列表
- Web Server
可以通过浏览器访问server url 查看和控制进程的状态
- xml-rpc接口
该接口可用于询问和控制主管及其运行的程序
安装:
sudo apt-get install supervosor
->配置文件已经在/etc/supervisor/supervisord.conf
# 主要修改以下内容
;[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) ;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 ; 'expected' exit codes for process (default 0,2) ;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 (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)
stopasgroup = true # 如果supervisor 管理的晋升px又产生了若干子进程,使用supervisor停止px京城,
停止信号会传播给px产生的所有子进程,确保子进程也一起停止。这一配置对希望停止所有进程的需求是非常有用的
autostart =true # 当启动supervisor的时候讲该配置项设置为true的所有进程自动启动
一个例子
[unix_http_server] file=/tmp/supervisor.sock ; (the path to the socket file) chmod=0777 [inet_http_server] ; HTTP 服务器,提供 web 管理界面 port=127.0.0.1:9001 ; Web 管理后台运行的 IP 和端口,如果开放到公网,需要注意安全性 username=admin ; 登录管理后台的用户名 password=1qaz@WSX? ; 登录管理后台的密码 [supervisord] logfile=/tmp/supervisord.log ; 日志文件,默认是 $CWD/supervisord.log logfile_maxbytes=50MB ; 日志文件大小,超出会 rotate,默认 50MB logfile_backups=10 ; 日志文件保留备份数量默认 10 loglevel=info ; 日志级别,默认 info,其它: debug,warn,trace pidfile=/tmp/supervisord.pid ; pid 文件 nodaemon=false ; 是否在前台启动,默认是 false,即以 daemon 的方式启动 minfds=1024 ; 可以打开的文件描述符的最小值,默认 1024 minprocs=200 ; 可以打开的进程数的最小值,默认 200 [rpcinterface:supervisor] supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface [supervisorctl] serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket ; 包含其他的配置文件 [program:xxx] command=python /home/ace/桌面/camera/sd/123.py
[program:tomcat] directory = /app/zpy/tomcat/bin/ ; 程序的启动目录 command = sh startup.sh ; 启动命令,可以看出与手动在命令行启动的命令是一样的 autostart = true ; 在 supervisord 启动的时候也自动启动 startsecs = 5 ; 启动 5 秒后没有异常退出,就当作已经正常启动了 autorestart = true ; 程序异常退出后自动重启 startretries = 3 ; 启动失败自动重试次数,默认是 3 user = zpy ; 用哪个用户启动 redirect_stderr = true ; 把 stderr 重定向到 stdout,默认 false stdout_logfile_maxbytes = 20MB ; stdout 日志文件大小,默认 50MB stdout_logfile_backups = 20 ; stdout 日志文件备份数 ; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件) stdout_logfile = /app/zpy/supervisor/usercenter_stdout.log
确保配置无误后可以再每台主机上使用以下的命令启动supervisor的服务端supervisord
sudo supervisord -c /etc/supervisor/supervisord.conf # 启动
supervisorctl shutdown # 停止supervisord
supervisorctl reload # 重新加载配置文件
进程管理
supervisorctl start all 启动管理的所有进程
supervisorctl stop all 停止所有
supervisorctl -c /etc/supervisor/supervisord.conf 需指定同一个配置文件
supervisorctl start program-name // program-name 为【program:xxx】
supervisorctl stop ...............
supervisorctl restart all //重启所有
supervisorctl restart program-name
supervisorctl status 产看当前管理的所有进程的状态
unix:///tmp/supervisor.sock no such file
出现上述错误的原因是supervisord并未启动,只要在命令行中使用命令sudo supervisord启动supervisord即可。