使用场景
通过对supervisord的学习,个人认为,如下的情况下,适合使用supervisord
1. 自己写的程序,没有启动脚本
2. 程序运行后,得用nohup进行后台运行以及日志输出
3. 一台服务器里面有多个程序,而且有优先级
上述场景下对应的好处:
1. 帮助你拥有启动和关闭的操作接口
2. 日志除了stdout显示之外,还可以输出到指定文件
3. 几个程序运行在一个节点里面,可以进行优先级调整
重要服务配置
- 主配置
[unix_http_server]
file=/tmp/supervisor.sock ; (the path to the socket file)
[inet_http_server] ; inet (TCP) server disabled by default
port=0.0.0.0:1009 ; (ip_address:port specifier, *:port for all iface)
username=ops ; (default is no username (open server))
password=123 ; (default is no password (open server))
[supervisord]
environment=LC_ALL='zh_CN.UTF-8',LANG='zh_CN.UTF-8'
logfile=/tmp/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10 ; (num of main logfile rotation backups;default 10)
loglevel=debug ; (log level;default info; others: debug,warn,trace)
pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false ; (start in foreground if true;default false)
minfds=1024 ; (min. avail startup file descriptors;default 1024)
minprocs=200 ; (min. avail process descriptors;default 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
[include]
files = /etc/supervisor.d/*.conf
- 子配置
[program:123]
priority=1 ; 数字越小,优先级越小,最先启动
command=/usr/local/123/bin/start2.sh
directory=/usr/local/123/bin/
autostart=true
autorestart=true
startretries=100
exitcodes=0,2 ;也是默认的数字
stopsignal=TERM ; 这个最狠,是10,我们都知道kill -9 这个更牛逼~
stopasgroup=true ; 是否想UNIX进程组发送结束信号 (default false)
killasgroup=true ; SIGKILL UNIX 进
stderr_logfile=/data/logs/123.error.log
stdout_logfile=/data/logs/123.info.log
- 尾声
一开始用supervisord的时候,遇到了几个坑。 一开始把service xx start直接放到了command里面,我擦,结果程序反复启动反复start, 一大堆,后来才知道,本身supervisord就是提供daemon功能的,所以只需要把start里面的命令拿出来就好,而且必须不能后台运行。
如果遇到程序无法restart的时候,要注意两个地方,一个是 exitstatus状态跟踪对了,另外一个是 kill选用 TERM这个最狠, 还有就是 killgroup啥的,一并也就true把,三管齐下,起码是够用了。 说的有点简单粗暴,细细耕作得慢慢来啦~
还有一个就是 网页查看乱码的问题,查看了google资料,解决办法如下:
[supervisord]
environment=LC_ALL='zh_CN.UTF-8',LANG='zh_CN.UTF-8'
参考:
https://groups.google.com/forum/#!topic/pylons-discuss/XiYfwlu-8q0