前几年本身用PHP写过一个服务守护的脚本,初步实现了被守护脚本的状态监控、优雅杀死、以及自动重启的功能。面试的时候也有问到,为何不使用supervisor
这个工具。由于当时项目少,并未思考那么多。目前项目中有使用supervisor 做为swoole 微服务的守护存在,所以准备深刻的学习下,可以实现微服务中的,服务状态监控功能。python
1:easy_install 安装: easy_install supervisor 2:pip 安装: pip install supervisor 3:Debian / Ubuntu能够直接经过apt安装: apt-get install supervisor
经过apt-get
安装的配置文件在:web
/etc/supervisor/supervisord.conf面试
其守护的进程,都转换微supervisor所管理的子进程,配置文件在:ubuntu
/etc/supervisor/conf.d安全
[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
这个下面的文件配置,以.conf
结尾,命名规则微:test.confbash
[program:app] ; 程序名称,在 supervisorctl 中经过这个值来对程序进行一系列的操做 autorestart=True ; 程序异常退出后自动重启 autostart=True ; 在 supervisord 启动的时候也自动启动 redirect_stderr=True ; 把 stderr 重定向到 stdout,默认 false environment=PATH="/home/app_env/bin" ; 能够经过 environment 来添加须要的环境变量,一种常见的用法是使用指定的 virtualenv 环境 command=python server.py ; 启动命令,与手动在命令行启动的命令是同样的 user=ubuntu ; 用哪一个用户启动 directory=/home/app/ ; 程序的启动目录
sudo supervisord -c /etc/supervisor/supervisord.conf
sudo supervisorctl > status # 查看程序状态 > stop usercenter # 关闭 usercenter 程序 > start usercenter # 启动 usercenter 程序 > restart usercenter # 重启 usercenter 程序 > reread # 读取有更新(增长)的配置文件,不会启动新添加的程序 > update # 重启配置文件修改过的程序