参考博文:http://www.ttlsa.com/linux/using-supervisor-control-program/
1.supervisord 介绍
Supervisor (http://supervisord.org) 是一个用 Python 写的进程管理工具,可以很方便的用来启动、重启、关闭进程(不仅仅是 Python 进程)。
除了对单个进程的控制,还可以同时启动、关闭多个进程,比如很不幸的服务器出问题导致所有应用程序都被杀死,此时可以用supervisor 同时启动所有应用程序而不是一个一个地敲命令启动。
2.supervisord 安装
1.supervisor 是 Python 编写的,所以安装起来也很方便,可以直接用 pip
1 2 3 4 5 6
| shell> yum install -y python-devel openssl openssl-devel shell> curl -O https://bootstrap.pypa.io/get-pip.py shell> python get-pip.py shell> pip install setuptools shell> pip install -U setuptools shell> pip install supervisor
|
2.安装完 supervisor 之后,可以运行echo_supervisord_conf 命令输出默认的配置项,也可以重定向到一个配置文件。
1
| shell> echo_supervisord_conf > /etc/supervisord.conf
|
3.修改配置文件,把 /etc/supervisord.conf 里 include 部分的的配置修改一下,目的是为了“一个进程对应一个管理的配置文件”,方便管理。
1 2 3
| shell> mkdir /etc/supervisor shell> sed -i 's#\;\[include\]#\[include\]#' /etc/supervisord.conf shell> sed -i '/relative\/directory/a\files = /etc/supervisor/*.ini' /etc/supervisord.conf
|
3.supervisord 管理进程配置
3.1 /etc/supervisord.conf 主配置文件说明
去除里面大部分注释和“不相关”的部分,我们可以先看这些配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| [unix_http_server] file=/tmp/supervisor.sock ; UNIX socket 文件,supervisorctl 会使用 ;chmod=0700 ; socket 文件的 mode,默认是 0700 ;chown=nobody:nogroup ; socket 文件的 owner,格式: uid:gid ;[inet_http_server] ; HTTP 服务器,提供 web 管理界面 ;port=127.0.0.1:9001 ; Web 管理后台运行的 IP 和端口,如果开放到公网,需要注意安全性 ;username=user ; 登录管理后台的用户名 ;password=123 ; 登录管理后台的密码 [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 ; 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 [supervisorctl] serverurl=unix:///tmp/supervisor.sock ; 通过 UNIX socket 连接 supervisord,路径与 unix_http_server 部分的 file 一致 ;serverurl=http://127.0.0.1:9001 ; 通过 HTTP 的方式连接 supervisord ; 包含其他的配置文件 [include] files = relative/directory/*.ini ; 可以是 *.conf 或 *.ini
|
3.1.管理进程配置说明
路径:/etc/supervisor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| [program:usercenter] directory = /home/leon/projects/usercenter ; 程序的启动目录 command = gunicorn -c gunicorn.py wsgi:app ; 启动命令,可以看出与手动在命令行启动的命令是一样的 autostart = true ; 在 supervisord 启动的时候也自动启动 startsecs = 5 ; 启动 5 秒后没有异常退出,就当作已经正常启动了 autorestart = true ; 程序异常退出后自动重启 startretries = 3 ; 启动失败自动重试次数,默认是 3 user = leon ; 用哪个用户启动 redirect_stderr = true ; 把 stderr 重定向到 stdout,默认 false stdout_logfile_maxbytes = 20MB ; stdout 日志文件大小,默认 50MB stdout_logfile_backups = 20 ; stdout 日志文件备份数 ; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件) stdout_logfile = /data/logs/usercenter_stdout.log ; 可以通过 environment 来添加需要的环境变量,一种常见的用法是修改 PYTHONPATH ; environment=PYTHONPATH=$PYTHONPATH:/path/to/somewhere
|
3.2 supervisord 进程管理配置文件示例
1 2 3 4 5 6 7 8 9 10 11 12 13
| [root@10 supervisor]# cat pc.yd.cc.ini [program:pc.yd.cc] directory = /data/wwwroot/pc.yd.cc command = python3 /data/wwwroot/pc.yd.cc/application.py autostart = true autorestart = true startretries = 5 stdout_logfile_maxbytes = 20MB stdout_logfile_backups = 20 stdout_logfile = /data/supervisord_logs/pc.yd.cc.log stderr_logfile_maxbytes = 20MB stderr_logfile_backups = 20 stderr_logfile = /data/supervisord_logs/pc.yd.cc.err
|
1 2 3 4 5 6 7 8 9 10 11 12
| [program:npc_filebeat_mysql] directory = /data/npcgame/npcgame_app_common/filebeat_mysql command = /data/npcgame/npcgame_app_common/filebeat_mysql/filebeats -e -c /data/npcgame/npcgame_app_common/filebeat_mysql/filebeat_mysql.yml autostart = true autorestart = true startretries = 5 stdout_logfile_maxbytes = 20MB stdout_logfile_backups = 20 stdout_logfile = /data/httplogs/npc_filebeat_mysql_access.log stderr_logfile_maxbytes = 20MB stderr_logfile_backups = 20 stderr_logfile = /data/httplogs/npc_filebeat_mysql_error.log
|
1 2 3 4 5 6 7 8 9 10 11
| [program:shadowsocks] command = /usr/local/bin/ssserver -c /etc/shadowsocks.json autostart = true autorestart = true startretries = 5 stdout_logfile_maxbytes = 20MB stdout_logfile_backups = 20 stdout_logfile = /data/shadowsocks/shadowsocks_access.log stderr_logfile_maxbytes = 20MB stderr_logfile_backups = 20 stderr_logfile = /data/shadowsocks/shadowsocks_error.log
|
4.supervisord 使用
Supervisord安装完成后有两个可用的命令行:
4.1.启动或关闭Supervisord
1 2 3 4 5 6 7
| # 初始启动Supervisord,启动、管理配置中设置的进程。 shell> supervisord 或 shell> /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
# 关闭supervisord shell> supervisorctl shutdown
|
4.2.验证Supervisord进程是否启动
1 2 3
| shell> ps aux|grep -i supervisord root 31932 0.0 0.0 103324 964 pts/1 S+ 09:57 0:00 grep -i supervisord root 32383 0.0 0.0 210480 14500 ? Ss Nov03 3:16 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
|
Supervisord的管理方式分为:
- shell命令行管理
- supervisorctl命令行管理
4.3.shell命令行管理supervisord
shell> supervisorctl 动作 进程名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| # 当进程的独立配置文件有变化时(增删进程),载入最新的配置文件。 shell> supervisorctl update
# 停止某一个进程(programxxx),programxxx为[program:blogdemon]里配置的值,这个示例就是blogdemon。 shell> supervisorctl stopp programxxx
# 启动某个进程 shell> supervisorctl start programxxx
# 重启某个进程 shell> supervisorctl restart programxxx
# 停止全部进程 shell> supervisorctl stop all
> 注:start、restart、stop都不会载入最新的配置文件。
# 载入最新的配置文件,并按新的配置启动、管理所有进程。 shell> supervisorctl reload
|
4.4.supervisorctl命令行管理
1 2 3 4 5 6 7 8 9
| # 会进入 supervisorctl 的shell界面,然后可以执行不同的命令了 shell> supervisorctl
> status # 查看进程状态 > stop programxxx # 关闭 usercenter 程序 > start programxxx # 启动 usercenter 程序 > restart programxxx # 重启 usercenter 程序 > reread # 读取有更新(增加)的配置文件,不会启动新添加的程序 > update # 重启配置文件修改过的程序
|
5.其他
除了 supervisorctl 之外,还可以配置 supervisrod 启动 web 管理界面,这个 web 后台使用 Basic Auth 的方式进行身份认证。
除了单个进程的控制,还可以配置 group,进行分组管理。
经常查看日志文件,包括 supervisord 的日志和各个 pragram 的日志文件,程序 crash 或抛出异常的信息一半会输出到 stderr,可以查看相应的日志文件来查找问题。
Supervisor 有很丰富的功能,还有其他很多项配置,可以在官方文档获取更多信息:http://supervisord.org/index.html