安装

平台要求

引自官网(supervised.org/introductio…):Supervisor已经过测试,可以在Linux(Ubuntu 9.10),Mac OS X(10.4 / 10.5 / 10.6)和Solaris(对于Intel为10)和FreeBSD 6.1上运行。它可能在大多数UNIX系统上都能正常工作。在任何版本的Windows下,Supervisor都不会运行。Supervisor可以使用Python 2.4或更高版本,但不能在任何版本的Python 3下使用。

安装

安装方法有:

  1. easy_install安装(需安装有pip):
$ easy_install supervisor
  1. pip安装(需安装有pip,推荐):
$ pip install supervisor
  1. Debian / Ubuntu可以直接通过apt安装:
$ apt-get install supervisor

安装后配置文件生成:

  1. 通过easy_install或pip安装后,需要运行
$ mkdir /etc/supervisor
$ echo_supervisord_conf > /etc/supervisor/supervisord.conf

生成配置文件。

  1. 使用apt-get安装后,主管的主配置文件在:
/etc/supervisor/supervisord.conf

主管的配置文件默认是不全的。子进程配置文件在:

/etc/supervisor/conf.d/*.conf

启动

我们先修改supervisord.conf最后的[include]部分配置:

[include]
files = /etc/supervisor/conf.d/*.conf

这样就可以支持子配置文件,而不用放入主配置文件。

  1. 使用pip或者easy_install安装的supervisor则:
$ supervisord

即可运行。

超级用户默认在以下路径查找配置文件中:/usr/etc/supervisord.conf、/usr/supervisord.conf、supervisord.conf等/supervisord.conf、/etc/supervisord.conf、/etc/supervisor/supervisord.conf

如果要指定主配置文件,则需要使用-c参数:

$ supervisord -c /etc/supervisor/supervisord.conf

查看安装的版本:

$ supervisord -v
3.3.4

查看supervisor的状态:

$ supervisorctl  status

注:supervisord是主进程,supervisorctl是给守护进程发送命令的客户端工具。

  1. 使用apt-get安装的直接supervisor可以通过
$ /etc/init.d/supervisor start

运行。

使用示例

我们以简单的/tmp/echo_time.sh为例:

#/bin/bashwhile true; doecho `date +%Y-%m-%d,%H:%m:%s`sleep 2
done

在/etc/supervisor/conf.d/补充子进程配置文件echo_time.conf:

[program:echo_time]
command=sh /tmp/echo_time.sh
priority=999                ; the relative start priority (default 999)
autostart=true              ; start at supervisord start (default: true)
autorestart=true            ; retstart at unexpected quit (default: true)
startsecs=10                ; number of secs prog must stay running (def. 10)
startretries=3              ; max # of serial start failures (default 3)
exitcodes=0,2               ; 'expected' exit codes for process (default 0,2)
stopsignal=QUIT             ; signal used to kill process (default TERM)
stopwaitsecs=10             ; max num secs to wait before SIGKILL (default 10)
user=root                 ; setuid to this UNIX account to run the program
log_stdout=true
log_stderr=true             ; if true, log program stderr (def false)
logfile=/tmp/echo_time.log
logfile_maxbytes=1MB        ; max # logfile bytes b4 rotation (default 50MB)
logfile_backups=10          ; # of logfile backups (default 10)
stdout_logfile_maxbytes=20MB  ; stdout 日志文件大小,默认 50MB
stdout_logfile_backups=20     ; stdout 日志文件备份数
stdout_logfile=/tmp/echo_time.stdout.log

然后启动程序:

$ supervisorctl reread
$ supervisorctl update

这两个命令分别代表重新重新配置,更新子进程组。执行更新后输出:

echo_time: added process group

这样刚才添加的脚本echo_time就常驻运行起来了。可以通过日志查看运行情况:

$ tail -f /tmp/echo_time.stdout.log
2018-12-22,14:12:1545459550
2018-12-22,14:12:1545459552
2018-12-22,14:12:1545459554

也可以使用supervisorctl status查看子进程运行情况:

$ supervisorctl  status
echo_time                        RUNNING   pid 28206, uptime 0:00:11

配置文件

主配置

主配置文件名:

可以通过运行echo_supervisord_conf获得。这个配置文件一般情况下不需要更改,除了最后的[include]部分,其余保持替换即可。

[unix_http_server]
file=/tmp/supervisor.sock   ; the path to the socket file
;chmod=0700                 ; socket file mode (default 0700)
;chown=nobody:nogroup       ; socket file uid:gid owner
;username=user              ; default is no username (open server)
;password=123               ; default is no password (open server);[inet_http_server]         ; 配置web后台
;port=127.0.0.1:9001        ; 指定ip_address:port, 使用 *:port 监听所有 IP
;username=user              ; 默认没有用户名 (open server)
;password=123               ; 默认没有密码 (open server)[supervisord]
logfile=/tmp/supervisord.log ; 日志文件; 默认 $CWD/supervisord.log
logfile_maxbytes=50MB        ; 日志文件最大大小; 默认 50MB
logfile_backups=10           ; # of main logfile backups; 0 means none, default 10
loglevel=info                ; log level; default info; others: debug,warn,trace
pidfile=/tmp/supervisord.pid ; pid文件
nodaemon=false               ; 是否运行在前台; 默认是后台
minfds=1024                  ; min. avail startup file descriptors; default 1024
minprocs=200                 ; min. avail process descriptors;default 200
;umask=022                   ; process file creation umask; default 022
;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; 配置 supervisorctl
; configure it match the settings in either the unix_http_server
; or inet_http_server section.[supervisorctl]
serverurl=unix:///tmp/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=123                ; 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; 下面是子进程配置文件示例
; 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=1                    ; number of processes copies to start (def 1)
;directory=/tmp                ; directory to cwd to before exec (def no cwd)
;umask=022                     ; umask for process (default None)
;priority=999                  ; the relative start priority (default 999)
;autostart=true                ; start at supervisord start (default: true)
;startsecs=1                   ; # of secs prog must stay up to be running (def. 1)
;startretries=3                ; max # of serial start failures when starting (default 3)
;autorestart=unexpected        ; when to restart if exited after running (def: unexpected)
;exitcodes=0,2                 ; 'expected' exit codes used with autorestart (default 0,2)
;stopsignal=QUIT               ; signal used to kill process (default TERM)
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;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=10     ; # of stdout logfile backups (0 means none, default 10)
;stdout_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
;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=10     ; # of stderr logfile backups (0 means none, default 10)
;stderr_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;environment=A="1",B="2"       ; 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=1                    ; number of processes copies to start (def 1)
;events=EVENT                  ; event notif. types to subscribe to (req'd)
;buffer_size=10                ; event buffer queue size (default 10)
;directory=/tmp                ; directory to cwd to before exec (def no cwd)
;umask=022                     ; umask for process (default None)
;priority=-1                   ; the relative start priority (default -1)
;autostart=true                ; start at supervisord start (default: true)
;startsecs=1                   ; # of secs prog must stay up to be running (def. 1)
;startretries=3                ; max # of serial start failures when starting (default 3)
;autorestart=unexpected        ; autorestart if exited after running (def: unexpected)
;exitcodes=0,2                 ; 'expected' exit codes used with autorestart (default 0,2)
;stopsignal=QUIT               ; signal used to kill process (default TERM)
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;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=10     ; # of stdout logfile backups (0 means none, default 10)
;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=10     ; # of stderr logfile backups (0 means none, default 10)
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;environment=A="1",B="2"       ; 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=999                  ; the relative start priority (default 999); 配置include files
; 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]
; .ini和.conf都支持
files = relative/directory/*.ini

子进程配置文件

一般放在:/etc/supervisor/conf.d/目录。一个脚本对应一个配置文件。

配置说明:

;*为必须填写项
;*[program:应用名称]
[program:cat];*命令路径,如果使用python启动的程序应该为 python /home/test.py,
;不建议放入/home/user/, 对于非user用户一般情况下是不能访问
command=/bin/cat;当numprocs为1时,process_name=%(program_name)s;
当numprocs>=2时,%(program_name)s_%(process_num)02d
process_name=%(program_name)s;进程数量
numprocs=1;执行目录,若有/home/supervisor_test/test1.py
;将directory设置成/home/supervisor_test
;则command只需设置成python test1.py
;否则command必须设置成绝对执行目录
directory=/tmp;掩码:--- -w- -w-, 转换后rwx r-x w-x
umask=022;优先级,值越高,最后启动,最先被关闭,默认值999
priority=999;如果是true,当supervisor启动时,程序将会自动启动
autostart=true;*自动重启
autorestart=true;启动延时执行,默认1秒
startsecs=10;启动尝试次数,默认3次
startretries=3;当退出码是0,2时,执行重启,默认值0,2
exitcodes=0,2;停止信号,默认TERM
;中断:INT(类似于Ctrl+C)(kill -INT pid),退出后会将写文件或日志(推荐)
;终止:TERM(kill -TERM pid)
;挂起:HUP(kill -HUP pid),注意与Ctrl+Z/kill -stop pid不同
;从容停止:QUIT(kill -QUIT pid)
;KILL, USR1, USR2其他见命令(kill -l),说明1
stopsignal=TERMstopwaitsecs=10;*以root用户执行
user=root;重定向
redirect_stderr=falsestdout_logfile=/a/path
stdout_logfile_maxbytes=1MB
stdout_logfile_backups=10
stdout_capture_maxbytes=1MB
stderr_logfile=/a/path
stderr_logfile_maxbytes=1MB
stderr_logfile_backups=10
stderr_capture_maxbytes=1MB;环境变量设置
environment=A="1",B="2"serverurl=AUTO

简化模板

[program:echo_time]
command=sh /tmp/echo_time.sh
autostart=true
autorestart=true
startsecs=10
startretries=3
exitcodes=0,2
stopsignal=QUIT
stopwaitsecs=10
user=root
log_stdout=true
log_stderr=true
logfile=/tmp/echo_time.log
logfile_maxbytes=1MB
logfile_backups=10
stdout_logfile_maxbytes=20MB
stdout_logfile_backups=20
stdout_logfile=/tmp/echo_time.stdout.log

或者

[program:x508server]
command=gunicorn x508server.wsgi:application -b 0.0.0.0:8000  ; 被监控的进程路径
directory=/home/webserver/web/WebServer/               ; 执行前要不要先cd到目录$
autostart=true                ; 随着supervisord的启动而启动
autorestart=true              ; 自动重启。。当然要选上了
startretries=10               ; 启动失败时的最多重试次数
exitcodes=0                   ; 正常退出代码
stopsignal=KILL               ; 用来杀死进程的信号
stopwaitsecs=10               ; 发送SIGKILL前的等待时间
redirect_stderr=true          ; 重定向stderr到stdout
stdout_logfile=/home/webserver/web/logfile.log        ; 指定日志文件
; 默认为 false,如果设置为 true,当进程收到 stop 信号时,会自动将该信号发给该进$
stopasgroup=true             ; send stop signal to the UNIX process
; 默认为 false,如果设置为 true,当进程收到 kill 信号时,会自动将该信号发给该进$
killasgroup=true             ; SIGKILL the UNIX process group (def false)

命令行程序

监督的

主管是主进程。

通过supervisord -h可以查看帮助说明。示例:-c/--configuration FILENAME ;指定配置文件
-n/--nodaemon ;运行在前台(调试用)
-v/--version ;打印版本信息-u/--user USER ;以指定用户(或用户ID)运行
-m/--umask UMASK ;指定子进程的umask,默认是022
-l/--logfile FILENAME ;指定日志文件
-e/--loglevel LEVEL ;指定日志级别

监督员

administratorctl是客户端程序,用于向supervisord发起命令。

通过supervisorctl -h可以查看帮助说明。我们主要关心的是其action命令:

$ supervisorctl  helpdefault commands (type help <topic>):
=====================================
add    exit      open  reload  restart   start   tail
avail  fg        pid   remove  shutdown  status  update
clear  maintail  quit  reread  signal    stop    version

这些命令对于控制子进程非常重要。示例:

reread ;重新加载配置文件
update ;将配置文件里新增的子进程加入进程组,如果设置了autostart=true则会启动新新增的子进程
status ;查看所有进程状态
status <name> ;查看指定进程状态
start all; 启动所有子进程
start <name>; 启动指定子进程
restart all; 重启所有子进程
restart <name>; 重启指定子进程
stop all; 停止所有子进程
stop <name>; 停止指定子进程
reload ;重启supervisord
add <name>; 添加子进程到进程组
reomve <name>; 从进程组移除子进程,需要先stop。注意:移除后,需要使用reread和update才能重新运行该进程

administratord有进程组(process group)的概念:只有子进程在进程组,才能被运行。

supervisorctl也支持统一命令行:

$ supervisorctl
echo_time                        RUNNING   pid 27188, uptime 0:05:09
supervisor> version
3.3.4
supervisor>

web界面操作

需要开启supervisord.conf注释掉的这4行:

[inet_http_server]         ; inet (TCP) server disabled by default
port=127.0.0.1:9001        ; ip_address:port specifier, *:port for all iface
username=user              ; default is no username (open server)
password=123               ; default is no password (open server)

体积最小是监听127.0.0.1:9001,这里方便测试,修改为:

port=*:9001

然后重启主进程supervisord:

$ supervisorctl reload

浏览器访问:http:// myip:9001,输入用户名,密码后,即可看到网页页面:

注意:如果修改配置文件时,[inet_http_server]这一行被注释,会导致另外的web需要认证,命令行使用supervisorctl也需要认证,这时候就需要在交互命令行里输入用户名,密码才能进行下一步的操作。

其他问题

  1. Centos6 docker环境没有pip

解决方案:需要先安装扩展源EPEL。

EPEL(fedoraproject.org/wiki/EPEL)是由Fedora社区打造,为RHEL及衍生发行版,如CentOS,Scientific Linux等提供大量的项目。

首先安装epel扩展源:

$ yum -y install epel-release

然后再安装pip

$ yum -y install python-pip

查看版本:

$ pip -V
pip 7.1.0 from /usr/lib/python2.6/site-packages (python 2.6)

参考:www.cnblogs.com/saolv/p/696 …

  1. Centos6环境安装supervisor后,执行:
$ supervisor -V

出现:

Traceback (most recent call last):File "/usr/bin/echo_supervisord_conf", line 5, in <module>from pkg_resources import load_entry_pointFile "/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg/pkg_resources.py", line 2603, in <module>File "/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg/pkg_resources.py", line 666, in requireFile "/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg/pkg_resources.py", line 565, in resolve
pkg_resources.DistributionNotFound: meld3>=0.6.5

原因:pip安装的meld3不可用,手动安装。

wget https://pypi.python.org/packages/source/m/meld3/meld3-1.0.2.tar.gz
tar -zxf meld3-1.0.2.tar.gz
cd meld3-1.0.2
python setup.py install

感谢https://www.cnblogs.com/hubery/p/5653007.html文章提供的方法!这个问题泄漏了好久,github页面(github.com/Supervisor/…。)。

  1. 因参考github里meld3解决方案导致pip不可用:
Traceback (most recent call last):File "/usr/bin/pip", line 5, in <module>from pkg_resources import load_entry_pointFile "/usr/lib/python2.6/site-packages/pkg_resources/__init__.py", line 957, in <module>class Environment:File "/usr/lib/python2.6/site-packages/pkg_resources/__init__.py", line 961, in Environmentself, search_path=None, platform=get_supported_platform(),File "/usr/lib/python2.6/site-packages/pkg_resources/__init__.py", line 188, in get_supported_platformplat = get_build_platform()File "/usr/lib/python2.6/site-packages/pkg_resources/__init__.py", line 391, in get_build_platformfrom sysconfig import get_platform
ImportError: No module named sysconfig

解决方案:删除/site-packages以下的包,重新安装python-setuptools:

rm -rf /usr/lib/python2.6/site-packages/pkg_resources*
yum reinstall python-setuptools

参考:stackoverflow.com/questions/5…

参考

  1. 一过程控制系统-监控3.3.4文档
  2. 进程管理的主管的简单说明- jyzhou -园博客
  3. 主管使用-回首郑板桥-博客园

supervised使用教程相关推荐

  1. 基于Python的完全监督机器学习教程 Complete Supervised Machine Learning With Python

    这门由行业和学术领袖开设的课程是为那些希望在数据科学领域建立有价值的职业生涯的人开设的 你会学到: 监督学习和非监督学习的原理及其区别. 线性和逻辑回归,决策树,回归树,随机森林,判别分析,支持向量机 ...

  2. QIIME 2教程. 15样品分类和回归q2-sample-classifier(2021.2)

    预测样本元数据q2-sample-classifier Predicting sample metadata values with q2-sample-classifier https://docs ...

  3. 有哪些LSTM(Long Short Term Memory)和RNN(Recurrent)网络的教程?

    知乎用户,阿里巴巴数据应用部门长期招聘「算法,分- 500 人赞同 刚好毕设相关,论文写完顺手就答了 先给出一个最快的了解+上手的教程: 直接看theano官网的LSTM教程+代码:LSTM Netw ...

  4. Stanford UFLDL教程 自编码算法与稀疏性

    自编码算法与稀疏性 目前为止,我们已经讨论了神经网络在有监督学习中的应用.在有监督学习中,训练样本是有类别标签的.现在假设我们只有一个没有带类别标签的训练样本集合 ,其中 .自编码神经网络是一种无监督 ...

  5. 超级大汇总!200多个最好的机器学习、NLP和Python教程

    这篇文章包含了我目前为止找到的最好的教程内容.这不是一张罗列了所有网上跟机器学习相关教程的清单--不然就太冗长太重复了.我这里并没有包括那些质量一般的内容.我的目标是把能找到的最好的教程与机器学习和自 ...

  6. 工业机器人入门实用教程_机器学习实用入门

    工业机器人入门实用教程 Following on from my earlier post on Data Science, here I will try to summarize and comp ...

  7. dcase_util教程(二)——各单元介绍

    接着上一篇教程,继续的有各个UTILITIES的介绍.网址 1. Container 数据容器的类.这些数据的目的是为了包装数据,使用有用的方法来访问和操作数据,以及加载和存储数据. 这些容器是从标准 ...

  8. UFLDL教程: Exercise:Self-Taught Learning

    自我学习 Deep Learning and Unsupervised Feature Learning Tutorial Solutions 1.先训练稀疏自编码器提取特征,再把特征和label给s ...

  9. caffe官网的部分翻译及NG的教程

    Caffe原来叫:Convolutional Architecture for Fast Feature Embedding 官网的个人翻译:http://blog.csdn.net/fengbing ...

最新文章

  1. $dbms=mysql_MySQL学习笔记(一)
  2. 我是怎么把一个项目带崩的
  3. Windows7中启动Mysql服务时提示:拒绝访问的一种解决方式
  4. Python Django 自定义Manager重写objects.create()方法代码示例
  5. php网站自动变暗,如何使用JS弹出DIV并使整个页面背景变暗
  6. 【转】Office2003与Office2007/2010共存方法
  7. DAY 34 进程通信、消费者模型和线程
  8. ks线切割编程系统3.13完整版-2次加密注册码
  9. Vue项目实战之电商后台管理系统(三) 用户管理模块
  10. 新蓝天羽网体育用品商店
  11. ETL——实现Kettle作业定时任务
  12. 大数据舆情分析:全方位解读《战狼2》
  13. rxjava背压_Android RxJava :图文详解 背压策略
  14. HBuilder封装APP
  15. Winform中给下拉框(ComboBox或LookUpEdit)添加内容
  16. 使用BadBoy录制JMeter脚本
  17. 云服务器购买按量付费实例,并搭建私有网络图解
  18. 小程序的自定义头,左上角显示图标
  19. MyEclipse注册码生成器
  20. Ubuntu18环境下编译MIT Cheetah-Software

热门文章

  1. AIOps的内容及工作原理介绍
  2. OA办公系统如何实现多维智能提醒
  3. JAVA计算机毕业设计防疫宣传系统Mybatis+源码+数据库+lw文档+系统+调试部署
  4. 报错 KeyError
  5. debian编译php mysql5.6_在Debian/Ubuntu上免编译安装PHP7.2~5.6
  6. java高级面试题总结
  7. 天不生我Alt,键道万古如长夜
  8. IGP 和 EGP 这两类协议的主要区别是什么?
  9. 高斯过程回归python_sklearn文档 — 1.7. 高斯过程
  10. 332. 重新安排行程(图,dfs(深度优先搜索))