centos7安装supervisor及配置文件详解

本文介绍在CentOS7上通过源码编译及YUM安装Supervisor的方法,并详细阐述如何配置supervisord.conf文件,实现服务的启动、停止、重启及状态查看等操作。

centos7安装supervisor:

    源码编译安装:

下载源码文件:supervisor-3.3.1.tar.gz
下载地址:https://pypi.python.org/pypi/supervisor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
安装:
[root@cenots7 src]# tar -zxf supervisor-3.3.1.tar.gz
[root@cenots7 src]# cd supervisor-3.3.1/
[root@cenots7 supervisor-3.3.1]# python setup.py install
 
 
检查是否安装成功:
登陆python控制台输入import supervisor 查看是否能成功加载
[root@cenots7 supervisor-3.3.1]# python
Python 2.7.5 (default, Sep 15 2016, 22:37:39) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help""copyright""credits" or "license" for more information.
>>> import supervisor
>>>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
生成配置文件:
[root@cenots7 supervisor-3.3.1]# mkdir /etc/supervisor
[root@cenots7 supervisor-3.3.1]# echo_supervisord_conf > /etc/supervisor/supervisord.conf
  
[root@cenots7 supervisor-3.3.1]# grep -E -v '^;|^$' /etc/supervisor/supervisord.conf
[unix_http_server]
file=/tmp/supervisor.sock   ; (the path to the socket file)
[supervisord]
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=info                ; (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


1
2
3
4
5
6
7
8
9
10
11
12
启动服务:
[root@cenots7 tmp]# supervisord -c /etc/supervisor/supervisord.conf
  
关闭服务:
[root@cenots7 tmp]# supervisorctl shutdown
Shut down
  
查看状态:
[root@cenots7 tmp]# supervisorctl status
 
启动或停止某一个服务:
supervisorctl stop|start program_name


    yum直接安装:

1
2
3
4
5
rpm -Uvh https://mirrors.ustc.edu.cn/fedora/epel/7/x86_64/e/epel-release-7-8.noarch.rpm
yum update;yum -y install supervisor
cat /etc/supervisord.conf
 
supervisord -c /etc/supervisord.conf


设置开机启动
systemctl enable supervisord.service
  • 1
配置文件
supervisord 的配置 文件是 /etc/supervisord.conf 
自定义配置文件目录是/etc/supervisord.d,该目录下文件已.ini为后缀
supervisord 命令
启动
systemctl start supervisord.service
  • 1
关闭
systemctl stop supervisord.service
  • 1
重启
systemctl restart supervisord.service
  • 1
配置进程
例如有个nginx 进程设置
vim /etc/supervisord.d/nginx.ini
  • 1
内容如下
[program:nginx];directory = /www/lanmps/bin ; 程序的启动目录command = /www/lanmps/bin/nginx start ; 启动命令,可以看出与手动在命令行启动的命令是一样的autostart = true ; 在 supervisord 启动的时候也自动启动startsecs = 5 ; 启动 5 秒后没有异常退出,就当作已经正常启动了autorestart = true ; 程序异常退出后自动重启startretries = 3 ; 启动失败自动重试次数,默认是 3user = www ; 用哪个用户启动redirect_stderr = true ; 把 stderr 重定向到 stdout,默认 falsestdout_logfile_maxbytes = 20MB ; stdout 日志文件大小,默认 50MBstdout_logfile_backups = 20 ; stdout 日志文件备份数; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)stdout_logfile = /www/logs/usercenter_stdout.logstopasgroup=false ;默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程killasgroup=false ;默认为false,向进程组发送kill信号,包括子进程; 可以通过 environment 来添加需要的环境变量,一种常见的用法是修改 PYTHONPATH; environment=PYTHONPATH=$PYTHONPATH:/path/to/somewhere

supervisorctl status # 状态supervisorctl stop nginx #关闭 nginxsupervisorctl start nginx #启动 nginxsupervisorctl restart nginx #重启 nginxsupervisorctl rereadsupervisorctl update #更新新的配置

其他
组成部分
supervisord:服务守护进程 
supervisorctl:命令行客户端 
Web Server:提供与supervisorctl功能相当的WEB操作界面 
XML-RPC Interface:XML-RPC接口
配置文件说明
配置文件/etc/supervisord.conf
[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.loglogfile_maxbytes=50MB ;日志文件大小,超出会rotate,默认 50MB,如果设成0,表示不限制大小logfile_backups=10 ;日志文件保留备份数量默认10,设为0表示不备份loglevel=info ;日志级别,默认info,其它: debug,warn,tracepidfile=/tmp/supervisord.pid ;pid 文件nodaemon=false ;是否在前台启动,默认是false,即以 daemon 的方式启动minfds=1024 ;可以打开的文件描述符的最小值,默认 1024minprocs=200 ;可以打开的进程数的最小值,默认 200[supervisorctl]serverurl=unix:///tmp/supervisor.sock ;通过UNIX socket连接supervisord,路径与unix_http_server部分的file一致;serverurl=http://127.0.0.1:9001 ; 通过HTTP的方式连接supervisord; [program:xx]是被管理的进程配置参数,xx是进程的名称[program:xx]command=/opt/apache-tomcat-8.0.35/bin/catalina.sh run ; 程序启动命令autostart=true ; 在supervisord启动的时候也自动启动startsecs=10 ; 启动10秒后没有异常退出,就表示进程正常启动了,默认为1秒autorestart=true ; 程序退出后自动重启,可选值:[unexpected,true,false],默认为unexpected,表示进程意外杀死后才重启startretries=3 ; 启动失败自动重试次数,默认是3user=tomcat ; 用哪个用户启动进程,默认是rootpriority=999 ; 进程启动优先级,默认999,值小的优先启动redirect_stderr=true ; 把stderr重定向到stdout,默认falsestdout_logfile_maxbytes=20MB ; stdout 日志文件大小,默认50MBstdout_logfile_backups = 20 ; stdout 日志文件备份数,默认是10; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)stdout_logfile=/opt/apache-tomcat-8.0.35/logs/catalina.outstopasgroup=false ;默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程killasgroup=false ;默认为false,向进程组发送kill信号,包括子进程;包含其它配置文件[include]files =/etc/supervisord.d/*.ini ;可以指定一个或多个以.ini结束的配置文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值