supervisor安装及其配置
一、supervisor概述
supervisor是一个c/s系统,被用来在类Unix系统中监控进程状态。supervisor使用python开发。 服务端进程为supervisord,主要负责启动自身及其监控的子进程,响应客户端命令,重启异常退出的子进程,记录子进程stdout和stderr输出,生成和处理子进程生命周期中的事件。其配置文件一般为/etc/supervisord.conf,可以在配置文件中配置相关参数,包括supervisord自身的状态,其管理的各个子进程的相关属性等。supervisor的客户端为supervisorctl,它提供了一个类shell的接口(即命令行)来操作supervisord服务端。通过supervisorctl,可以连接到supervisord服务进程,获得服务进程监控的子进程状态,启动和停止子进程,获得正在运行的进程列表。客户端通过Unix域套接字或者TCP套接字与服务进程进行通信,服务器端具有身份凭证认证机制,可以有效提升安全性。当客户端和服务端位于同一台机器上时,客户端与服务器共用同一个配置文件/etc/supervisord.conf,通过不同标签来区分两者的配置。supervisor也提供了一个web页面来查看和管理进程状态。
二、supervisor安装及相关配置
(1)安装
wget https://pypi.python.org/packages/7b/17/88adf8cb25f80e2bc0d18e094fcd7ab300632ea00b601cbbbb84c2419eae/supervisor-3.3.2.tar.gz#md5=04766d62864da13d6a12f7429e75314f
tar zxvf supervisor-3.3.2.tar.gz && cd supervisor-3.3.2
python setup.py install
supervisor安装完成后会生成三个执行程序:supervisortd、supervisorctl以及echo_supervisord_conf,它们分别是supervisor的守护进程服务(用于接收进程管理命令)、
客户端(用于和守护进程通信,发送管理进程的指令)以及生成初始配置文件程序。
(2)配置
运行supervisord服务的时候,需要指定supervisor配置文件,如果没有显示指定,默认在以下目录或文件中查找(其中$CWD表示运行supervisord程序的目录):
$CWD/supervisord.conf
$CWD/etc/supervisord.conf
/etc/supervisord.conf
/etc/supervisor/supervisord.conf (since Supervisor 3.3.0)
../etc/supervisord.conf (Relative to the executable)
../supervisord.conf (Relative to the executable)
安装完成后,可以通过运行echo_supervisord_conf程序生成supervisor的初始化配置文件,如下所示:
echo_supervisord_conf > /etc/supervisord.conf 生成supervisor的主配置文件
mkdir /etc/supervisord.d 用户存放被监控进程的配置文件
(3)配置文件参数说明
supervisor的配置参数较多,详细的配置参数说明请参考官方文档介绍,下面介绍一些常用的参数配置,分号(;)开头的配置表示注释。
[unix_http_server] ;[inet_http_server] ; 侦听在TCP上的socket,Web Server和远程的supervisorctl都要用到它,如果不设置,默认为不开启。非必须设置项 [supervisord] ;主要定义服务端进程supervisord的相关属性。必须设置项 ; the below section must remain in the config file for RPC [rpcinterface:supervisor] ;该参数为XML_RPC服务,如果使用supervisord或者web server,该选项必须要开启 [supervisorctl] ;主要针对supervisorctl的一些属性配置 ;username=chris ; 连接时用户名,默认为空。非必须设置项 ; The below sample program section shows all possible program subsection values, ;[program:theprogramname] ; 管理的子进程,":"后面是子进程名字,最好和实际进程相关联。program可以设置一个或多个,一个program就是一个要被管理的进程 ; The below sample eventlistener section shows all possible ;[eventlistener:theeventlistenername] ;与program功能类似,也是suopervisor启动的子进程,不过它是订阅supervisord发送的event。它的名字就叫 ; The below sample group section shows all possible group values, ;[group:thegroupname] ; 给programs分组,划分到组里面的program。设置后就不用一个一个去操作了我们可以对组名进行统一的操作。 ; The [include] section can just contain the "files" setting. This ;[include] ; 有用的配置项,当管理的进程很多时,写一个配置文件就会很多,不够清晰。 include示例: |
三、配置管理进程
进程管理配置参数,不建议全都写在supervisord.conf文件中,应该每个进程写一个配置文件放在include指定的目录下,并包含进supervisord.conf文件中。
创建/etc/supervisord.d目录,用于存放进程管理的配置文件
修改/etc/supervisord.conf中的include参数,将/etc/supervisor.d目录添加到include中,实例如下
; Sample supervisor config file.
;
; For more information on the config file, please see:
; http://supervisord.org/configuration.html
;
; Notes:
; - Shell expansion ("~" or "$HOME") is not supported. Environment
; variables can be expanded using this syntax: "%(ENV_HOME)s".
; - Quotes around values are not supported, except in the case of
; the environment= options as shown below.
; - Comments must have a leading space: "a=b ;comment" not "a=b;comment".
; - Command will be truncated if it looks like a config file comment, e.g.
; "command=bash -c 'foo ; bar'" will truncate to "command=bash -c 'foo ". [unix_http_server]
file=/var/run/supervisor.sock ; the path to the socket file
#chmod= ; socket file mode (default )
#chown=nobody:nogroup ; socket file uid:gid owner
#username=user ; default is no username (open server)
#password= ; default is no password (open server) [inet_http_server] ; inet (TCP) server disabled by default
port=*: ; ip_address:port specifier, *:port for all iface
username=user ; default is no username (open server)
password= ; default is no password (open server) [supervisord]
logfile=/var/log/supervisord.log ; main log file; default $CWD/supervisord.log
logfile_maxbytes=50MB ; max main logfile bytes b4 rotation; default 50MB
logfile_backups= ; # of main logfile backups; means none, default
loglevel=info ; log level; default info; others: debug,warn,trace
pidfile=/var/run/supervisord.pid ; supervisord pidfile; default supervisord.pid
nodaemon=False ; start in foreground if true; default false
minfds= ; min. avail startup file descriptors; default
minprocs= ; min. avail process descriptors;default
;umask= ; process file creation umask; default
;user=chrism ; default is current user, required if root
;identifier=supervisor ; supervisord identifier, default is 'supervisor'
;directory=/tmp ; default is not to cd during start
;nocleanup=true ; don't clean up tempfiles at start; default false
;childlogdir=/tmp ; 'AUTO' child log dir, default $TEMP
;environment=KEY="value" ; key value pairs to add to environment
;strip_ansi=false ; strip ansi escape codes in logs; def. false ; The rpcinterface:supervisor 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:x] sections. [rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface ; The supervisorctl section configures how supervisorctl will connect to
; supervisord. configure it match the settings in either the unix_http_server
; or inet_http_server section. [supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL for a unix socket
;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
;username=chris ; should be same as in [*_http_server] if set
;password= ; should be same as in [*_http_server] if set
;prompt=mysupervisor ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history ; use readline history if available ; The sample program section below shows all possible program subsection values.
; Create one or more 'real' program: sections to be able to control them under
; supervisor. ;[program:theprogramname]
;command=/bin/cat ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs= ; number of processes copies to start (def )
;directory=/tmp ; directory to cwd to before exec (def no cwd)
;umask= ; umask for process (default None)
;priority= ; the relative start priority (default )
;autostart=true ; start at supervisord start (default: true)
;startsecs= ; # of secs prog must stay up to be running (def. )
;startretries= ; max # of serial start failures when starting (default )
;autorestart=unexpected ; when to restart if exited after running (def: unexpected)
;exitcodes=, ; 'expected' exit codes used with autorestart (default ,)
;stopsignal=QUIT ; signal used to kill process (default TERM)
;stopwaitsecs= ; max num secs to wait b4 SIGKILL (default )
;stopasgroup=false ; send stop signal to the UNIX process group (default false)
;killasgroup=false ; SIGKILL the UNIX process group (def false)
;user=chrism ; setuid to this UNIX account to run the program
;redirect_stderr=true ; redirect proc stderr to stdout (default false)
;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups= ; # of stdout logfile backups ( means none, default )
;stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default )
;stdout_events_enabled=false ; emit events on stdout writes (default false)
;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups= ; # of stderr logfile backups ( means none, default )
;stderr_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default )
;stderr_events_enabled=false ; emit events on stderr writes (default false)
;environment=A="",B="" ; process environment additions (def no adds)
;serverurl=AUTO ; override serverurl computation (childutils) ; The sample eventlistener section below shows all possible eventlistener
; subsection values. Create one or more 'real' eventlistener: sections to be
; able to handle event notifications sent by supervisord. ;[eventlistener:theeventlistenername]
;command=/bin/eventlistener ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs= ; number of processes copies to start (def )
;events=EVENT ; event notif. types to subscribe to (req'd)
;buffer_size= ; event buffer queue size (default )
;directory=/tmp ; directory to cwd to before exec (def no cwd)
;umask= ; umask for process (default None)
;priority=- ; the relative start priority (default -)
;autostart=true ; start at supervisord start (default: true)
;startsecs= ; # of secs prog must stay up to be running (def. )
;startretries= ; max # of serial start failures when starting (default )
;autorestart=unexpected ; autorestart if exited after running (def: unexpected)
;exitcodes=, ; 'expected' exit codes used with autorestart (default ,)
;stopsignal=QUIT ; signal used to kill process (default TERM)
;stopwaitsecs= ; max num secs to wait b4 SIGKILL (default )
;stopasgroup=false ; send stop signal to the UNIX process group (default false)
;killasgroup=false ; SIGKILL the UNIX process group (def false)
;user=chrism ; setuid to this UNIX account to run the program
;redirect_stderr=false ; redirect_stderr=true is not allowed for eventlisteners
;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups= ; # of stdout logfile backups ( means none, default )
;stdout_events_enabled=false ; emit events on stdout writes (default false)
;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups= ; # of stderr logfile backups ( means none, default )
;stderr_events_enabled=false ; emit events on stderr writes (default false)
;environment=A="",B="" ; process environment additions
;serverurl=AUTO ; override serverurl computation (childutils) ; The sample group section below shows all possible group values. Create one
; or more 'real' group: sections to create "heterogeneous" process groups. ;[group:thegroupname]
;programs=progname1,progname2 ; each refers to 'x' in [program:x] definitions
;priority= ; the relative start priority (default ) ; The [include] section can just contain the "files" setting. This
; setting can list multiple files (separated by whitespace or
; newlines). It can also contain wildcards. The filenames are
; interpreted as relative to this file. Included files *cannot*
; include files themselves. [include]
files = /etc/supervisord.d/*.conf
/etc/supervisord.conf
#为了方便管理,增加一个tornado组
[group:tornados]
programs=tornado-,tornado- # 分别定义两个tornado的进程配置
[program:tornado-]
# 进程要执行的命令
#command=python /home/mcp/tornado/hello.py --port=
command=python /home/mcp/tornado/hello.py
directory=/home/mcp/tornado/
user=mcp
autostart=true
# 自动重启
autorestart=true
redirect_stderr=true
# 日志路径
stdout_logfile=/home/mcp/tornado/tornado0.log
loglevel=info [program:tornado-]
#command=python /home/mcp/tornado/hello.py --port=
command=python /home/mcp/tornado/hello.py
directory=/home/mcp/tornado/
user=mcp
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/home/mcp/tornado/tornado1.log
loglevel=info
supervisor_tornado.conf
四、supervisor相关命令
supervisord相关命令:
supervisord 启动服务端进程
/usr/bin/supervisord -c /etc/supervisord.conf 按指定配置文件启动服务端进程
supervisorctl相关命令:
supervisorctl 进入交互界面
supervisorctl status 查看被监控进程状态
supervisorctl stop all 关闭被监控的进程
supervisorctl start all 启动被监控的进程
supervisorctl start program-name 其中program-name为配置文件[program:xx]中的xx
supervisorctl stop program-name 其中program-name为配置文件[program:xx]中的xx
supervisorctl restart all 重启被监控的进程
supervisorctl reatart program-name 重启某一进程,program-name为[program:xx]中的xx
supervisorctl shutdown 关闭supervisord服务端
supervisorctl reload 重新加载配置文件
五、把supervisor加入开机自启动服务(CentOS7.X系统)
(1)利用/etc/rc.local
echo "/usr/bin/supervisord -c /etc/supervisord.conf" >> /etc/rc.local
/etc/rc.local -> rc.d/rc.local /etc/rc.local是/etc/rc.d/rc.local的软连接
如果开机启动不生效,则首先需要检查下/etc/rc.d/rc.local是否具有可执行权限
(2)加入systemctl管理
vim /lib/systemd/system/supervisor.service
[Unit]
Description=supervisor
After=network.target [Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf
ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown
ExecReload=/usr/bin/supervisorctl $OPTIONS reload
KillMode=process
Restart=on-failure
RestartSec=42s [Install]
WantedBy=multi-user.target
上述文件编写后,执行如下命令即可:
systemctl enable supervisor.service 加入开机自启动服务
systemctl daemon-reload 重新载入systemd,扫描新的或有变动的单元(必要步骤)
chmod 766 /lib/systemd/system/supervisor.service 修改文件权限
六、把supervisor加入systemctl管理
通过上述(五),实际上supervisor已经加入了systemctl管理了,后续起停supervisor服务都可以通过systemctl来控制了
systemctl start supervisor.service 启动服务
systemctl stop supervisor.service 停止服务
systemctl restart supervisor.service 重新启动服务
systemctl reload supervisor.service 重载配置文件
systemctl status supervisor.service 查看服务状态(显示的类似于操作记录)
supervisor安装及其配置的更多相关文章
- Supervisor 安装与配置
Supervisor是一个进程监控程序. 满足的需求是:我现在有一个进程需要每时每刻不断的跑,但是这个进程又有可能由于各种原因有可能中断.当进程中断的时候我希望能自动重新启动它,此时,我就需要使用到了 ...
- supervisor安装和配置
直接命令 easy_install supervisor 如果报错先安装 yum install python-setuptools,再上面一条命令: 安装成功后显示finished,我们再次进行py ...
- Supervisor安装与配置(Linux/Unix进程管理工具)
原文链接:http://blog.csdn.net/xyang81/article/details/51555473 Supervisor(http://supervisord.org/)是用Pyth ...
- Supervisor安装与配置
Supervisor(http://supervisord.org/)是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统 ...
- CentOS7下Supervisor安装与配置
Supervisor(http://supervisord.org/)是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统 ...
- supervisor 安装、配置、常用命令
前言 在 web 应用部署到线上后,需要保证应用一直处于运行状态,在遇到程序异常.报错等情况,导致 web 应用终止时,需要保证程序可以立刻重启,继续提供服务. 所以,就需要一个工具,时刻监控 web ...
- 【转】supervisor安装与配置
1.安装 宿主机环境:(Centos7) 宿主机环境 #yum install python-setuptools yum install python-setuptools#easy_install ...
- Supervisor安装、配置、开启启动
1.安装Python包管理工具(easy_install) wget --no-check-certificate https://bootstrap.pypa.io/ez_setup.py -O - ...
- supervisor安装、配置和运行
supervisor是python写的进程管理工具,supervisor能够批量对进程执行启动,停止,重启等操作,有效提高了运维效率.注意supervisor只能管理前台进程,supervisor会自 ...
随机推荐
- iOS设计模式之类族(class cluster)
类族模式在UIKit(user interface framework)使用的范围已经远远超过我们的想象,比如,UIButton,NSArray,NSString,NSNumber等, 例如NSNum ...
- Java多线程详解(二)
评论区留下邮箱可获得<Java多线程设计模式详解> 转载请指明来源 1)后台线程 后台线程是为其他线程服务的一种线程,像JVM的垃圾回收线程就是一种后台线程.后台线程总是等到非后台线程死亡 ...
- (转)从程序员到CTO
好好努力吧,向优秀的人看齐.文章来自http://blog.csdn.net/smarttony/article/details/6697617
- ubuntu 14.04 返回到经典桌面方法
1.打开终端,运行下面命令:sudo apt-get install gnome-session-fallback 2.重启机器,选择gnome,然后登录
- Unity3D之Unity3D 4.3.0 破解方法
Dear All 破解有风险,破解不尊重知识产权,如果有涉及请删除或者联系我……以下呢 是我这几天捣鼓的4.3.0版本 供学习!请大家支持正版! 1.下载最新版本 我是在Unity官网下载的最新版本 ...
- WCF(四) 深入契约
服务契约中的请求-响应操作 1.请求-响应模式 [OperationContract]//1默认就是 请求-相应 Requst- Replay DateTime GetDateTime(); [Ope ...
- 树形DP求树的直径
hdu4607 Park Visit Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- vue监听滚动事件
vue中监听滚动事件,然后对其进行事件处理,一般有:1. 滚动到顶部吸附: 2. 根据滚动的位置激活对应的tab键(锚链接tab键) 这两种方式的处理都是可通过监听scroll来实现 mounted( ...
- Nginx配置ssl安全证书
server { listen 443; server_name www.loaclhost.com; ssl on; root /web; ssl_certificate /data/ssl/xxx ...
- pta习题集5-16 朋友圈
某学校有N个学生,形成M个俱乐部.每个俱乐部里的学生有着一定相似的兴趣爱好,形成一个朋友圈.一个学生可以同时属于若干个不同的俱乐部.根据"我的朋友的朋友也是我的朋友"这个推论可以得 ...