Supervisor的作用与配置

supervisor管理进程,是通过fork/exec的方式将这些被管理的进程当作supervisor的子进程来启动,所以我们只需要将要管理进程的可执行文件的路径添加到supervisor的配置文件中就好了。此时被管理进程被视为supervisor的子进程,若该子进程异常中断,则父进程可以准确的获取子进程异常中断的信息,可以选择是否自己启动和报警。通过在配置文件中设置autostart=ture,可以实现对异常中断的子进程的自动重启。基于Python语言开发

安装supervisor

sudo apt-get install supervisor

supervisor的配置文件在:

/etc/supervisor/supervisord.conf

supervisor的配置文件默认是不全的,不过在大部分默认的情况下,上面说的基本功能已经满足。

安装完supervisor后,输入以下命令可得到配置文件:

echo_supervisord_conf

常用应用让supervisor来管理它,放在/etc/supervisor/conf.d/目录下,以.conf作为扩展名

vim /etc/supervisor/conf.d/nginx.conf
[program:nginx]
command = /usr/sbin/nginx -g 'daemon off;'
startsecs=0
autostart=true
autorestart=true
stdout_logfile=/var/log/nginx_sup.log
stopasgroup=true
killasgroup=true

vim /etc/supervisor/conf.d/elasticsearch.conf
[program:elasticsearch]
command = su - elasticsearch -c "/data/product/service/elasticsearch-5.4.2/bin/elasticsearch -d"
autostart = true
autorestart = true
stopwaitsecs=1
startsecs=1
user=elasticsearch 
stopasgroup = true
stdout_logfile = /var/log/elasticsearch.log
stderr_logfile = /var/log/redis/elasticsearcherr.log

vim /etc/supervisor/conf.d/kafka.conf
[program:kafka]
command=/data/product/service/kafka_2.11-0.8.2.2/bin/kafka-server-start.sh  /data/product/service/kafka_2.11-0.8.2.2/config/server.properties
autostart=true
autorestart=true
stopwaitsecs=1
stopasgroup=true
startsecs=50
user=root
stderr_logfile=/var/log/kafkaerr.log
stdout_logfile=/var/log/kafka.log

vim /etc/supervisor/conf.d/mysql.conf
[program:mysql]
command=/usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
autorestart=unexpected
stopwaitsecs=1
stopasgroup=true
user=mysql
stderr_logfile=/tmp/err.log
std_logfile=/tmp/out.log

vim /etc/supervisor/conf.d/redis.conf
[program:redis]
command=/usr/bin/redis-server
autostart=true
autorestart=true
stopwaitsecs=1
startsecs=3
user=root
stopasgroup=true
stdout_logfile=/var/log/redis/redis.log
stderr_logfile=/var/log/redis/redis.log

vim /etc/supervisor/conf.d/storm.conf
[program:storm]
command = /data/product/service/apache-storm-1.1.0/bin/storm nimbus
autostart = true
autorestart = true
stopwaitsecs = 1
user = root
stopasgroup = true
stderr_logfile = /var/log/stormerr.log
std_logfile = /var/log/storm.log

vim /etc/supervisor/conf.d/zookeeper.conf
[program:zookeeper]
command=/data/product/service/zookeeper-3.4.10/bin/zkServer.sh  start-foreground  zoo-1.cfg 
autorestart=unexpected
stopwaitsecs=1
stopasgroup=true
stderr_logfile=/var/log/zookeeper.log
std_logfile=/var/log/zookeepererr.log

给自己需要的脚本程序编写一个子进程配置文件

#项目名
[program:blog]
#脚本目录
directory=/opt/bin
#脚本执行命令
command=/usr/bin/php /data/www/test.php
#supervisor启动的时候是否随着同时启动,默认True
autostart=true
#当程序exit的时候,这个program不会自动重启,默认unexpected
#设置子进程挂掉后自动重启的情况,有三个选项,false,unexpected和true。如果为false的时候,无论什么情况下,都不会被重新启动,如果为unexpected,只有当进程的退出码不在下面的exitcodes里面定义的
autorestart=false
#这个选项是子进程启动多少秒之后,此时状态如果是running,则我们认为启动成功了。默认值为1
startsecs=1
#日志输出 
stderr_logfile=/tmp/blog_stderr.log 
stdout_logfile=/tmp/blog_stdout.log 
#脚本运行的用户身份 
user = zhoujy 
#把 stderr 重定向到 stdout,默认 false
redirect_stderr = true
#stdout 日志文件大小,默认 50MB
asgfjfghfc_maxbytes = 20M
#stdout 日志文件备份数
stdout_logfile_backups = 20

[program:zhoujy] #说明同上
directory=/opt/bin 
command=/usr/bin/python /opt/bin/zhoujy.py 
autostart=true 
autorestart=false 
stderr_logfile=/tmp/zhoujy_stderr.log 
stdout_logfile=/tmp/zhoujy_stdout.log 
#user = zhoujy

关闭、开启指定的子进程

supervisorctl stop zhoujy
supervisorctl start zhoujy

supervisor基本命令(后四个命令可以省略"-c supervisor.conf"):

supervisord -c supervisor.conf                       通过配置文件启动supervisor
supervisorctl -c supervisor.conf status              查看状态
supervisorctl -c supervisor.conf reload              重新载入配置文件
supervisorctl -c supervisor.conf start [all]|[x]     启动所有/指定的程序进程
supervisorctl -c supervisor.conf stop [all]|[x]      关闭所有/指定的程序进程 

开启web界面操作

[inet_http_server]
port=127.0.0.1:9001 ;
username=user       ;
password=123        ;

通过http://localhost:9001访问如下图 如果supervisor遇到错误,可以在/var/log/supervisor/supervisord.log中查看日志;
如果app运行出现问题,可以在/srv/awesome/log/app.log中查看日志。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值