一、定义:Supervisor在百度百科上给的定义是超级用户,监管员。Supervisor是一个过程管理工具,当过程中断的时候Supervisor能主动重新启动它。能够运行在各种类unix的机器上,supervisor就是用Python开发的一套通用的过程管理程序,能将一个一般的命令行过程变为后盾daemon,并监控过程状态,异样退出时能主动重启。
二、介绍Supervisor
- supervisord
运行 Supervisor 时会启动一个过程 supervisord,它负责启动所治理的过程,并将所治理的过程作为本人的子过程来启动,而且能够在所治理的过程呈现解体时主动重启。
- supervisorctl
是命令行管理工具,能够用来执行 stop、start、restart 等命令,来对这些子过程进行治理。
supervisor是所有过程的父过程,治理着启动的子停顿,supervisor以子过程的PID来治理子过程,当子过程异样退出时supervisor能够收到相应的信号量。
三、装置Supervisor
1、yum装置:yum install supervisor
2、python装置:yum install python-setuptools,easy_install supervisor
3、下载包装置
wget https://pypi.python.org/packages/source/s/supervisor/supervisor-3.1.3.tar.gztar zxvf supervisor-3.1.3.tar.gzcd supervisor-3.1.3python setup.py install
四、查看是否装置胜利
执行命令 echo_supervisord_conf
输入主配置文件内容
五、初始化配置文件
mkdir /usr/supervisorecho_supervisord_conf > /usr/supervisor/supervisord.conf
主配置文件解释
[root@centos-011 ~ 07:50:00]#cat /etc/supervisord.conf.bak; Sample supervisor config file. [unix_http_server]file=/var/run/supervisor/supervisor.sock ; socket 门路 ;chmod=0700 ; socket 文件的权限;chown=nobody:nogroup ; socket 所属用户及组;username=user ; 用户名;password=123 ; 明码 ;[inet_http_server] ; 是否启用服务,默认是敞开的(启用的话能够看到supervisor 治理的服务状态);port=127.0.0.1:9001 ; 监听的IP及端口;username=user ; 用户名;password=123 ; 明码 [supervisord] ; supervisord 全局配置logfile=/var/log/supervisor/supervisord.log ; supervisor 日志门路logfile_maxbytes=50MB ; 单个日志文件最大数logfile_backups=10 ; 保留多少个日志文件(默认10个)loglevel=info ; (log level;default info; others: debug,warn,trace)pidfile=/var/run/supervisord.pid ; pid 文件门路nodaemon=false ; 启动是否丢到前台,设置为false ,示意以daemon 的形式启动minfds=1024 ; 最小文件关上数,对应零碎limit.conf 中的nofile ,默认最小为1024,最大为4096minprocs=200 ; 最小的过程关上数,对应零碎的limit.conf 中的nproc,默认为200;umask=022 ; (process file creation umask;default 022);user=chrism ; 启动supervisord 服务的用户,默认为root;identifier=supervisor ; (supervisord identifier, default is 'supervisor');directory=/tmp ; 这里的目录指的是服务的工作目录;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 below 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: sections[rpcinterface:supervisor]supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface [supervisorctl]serverurl=unix:///var/run/supervisor/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 http_username if set;password=123 ; should be same as http_password if set;prompt=mysupervisor ; cmd line prompt (default "supervisor");history_file=~/.sc_history ; use readline history if available ; The below sample program section shows all possible program subsection values,; create one or more 'real' program: sections to be able to control them under; supervisor. ;[program:theprogramname] ; 定义一个守护过程 ,比方上面的elasticsearch ;command=/bin/cat ; 启动程序应用的命令,能够是绝对路径或者相对路径;process_name=%(program_name)s ; 一个python字符串表达式,用来示意supervisor过程启动的这个的名称,默认值是%(program_name)s;numprocs=1 ; Supervisor启动这个程序的多个实例,如果numprocs>1,则process_name的表达式必须蕴含%(process_num)s,默认是1;directory=/tmp ; supervisord在生成子过程的时候会切换到该目录;umask=022 ; umask for process (default None);priority=999 ; 权重,能够控制程序启动和敞开时的程序,权重越低:越早启动,越晚敞开。默认值是999;autostart=true ; 如果设置为true,当supervisord启动的时候,过程会主动启动;autorestart=true ; 设置为随 supervisord 重启而重启,值能够是false、true、unexpected。false:过程不会主动重启;startsecs=10 ; 程序启动后期待多长时间后才认为程序启动胜利,默认是10秒;startretries=3 ; supervisord尝试启动一个程序时尝试的次数。默认是3;exitcodes=0,2 ; 一个预期的退出返回码,默认是0,2。;stopsignal=QUIT ; 当收到stop申请的时候,发送信号给程序,默认是TERM信号,也能够是 HUP, INT, QUIT, KILL, USR1, or USR2;stopwaitsecs=10 ; 在操作系统给supervisord发送SIGCHILD信号时期待的工夫;user=chrism ; 如果supervisord以root运行,则会应用这个设置用户启动子程序;redirect_stderr=true ; 如果设置为true,过程则会把规范谬误输入到supervisord后盾的规范输入文件描述符;stdout_logfile=/a/path ; 把过程的规范输入写入文件中,如果stdout_logfile没有设置或者设置为AUTO,则supervisor会主动抉择一个文件地位;stdout_logfile_maxbytes=1MB ; 规范输入log文件达到多少后主动进行轮转,单位是KB、MB、GB。如果设置为0则示意不限度日志文件大小;stdout_logfile_backups=10 ; 规范输入日志轮转备份的数量,默认是10,如果设置为0,则不备份;stdout_capture_maxbytes=1MB ; 当过程处于stderr capture mode模式的时候,写入FIFO队列的最大bytes值,单位能够是KB、MB、GB;stdout_events_enabled=false ; 如果设置为true,当过程在写它的stderr;stderr_logfile=/a/path ; 把过程的谬误日志输入一个文件中,除非redirect_stderr参数被设置为true;stderr_logfile_maxbytes=1MB ; 谬误log文件达到多少后主动进行轮转,单位是KB、MB、GB。如果设置为0则示意不限度日志文件大小;stderr_logfile_backups=10 ; 谬误日志轮转备份的数量,默认是10,如果设置为0,则不备份;stderr_capture_maxbytes=1MB ; 当过程处于stderr capture mode模式的时候,写入FIFO队列的最大bytes值,单位能够是KB、MB、GB;stderr_events_enabled=false ; 如果设置为true,当过程在写它的stderr到文件描述符的时候,PROCESS_LOG_STDERR事件会被触发;environment=A=1,B=2 ; 一个k/v对的list列表;serverurl=AUTO ; 是否容许子过程和外部的HTTP服务通信,如果设置为AUTO,supervisor会主动的结构一个url ; The below sample eventlistener section shows all possible; eventlistener subsection values, create one or more 'real'; eventlistener: sections to be able to handle event notifications; sent by supervisor. #这个中央是自定义一个守护过程[program:elasticsearch] ; 定义一个守护过程 elasticsearchenvironment=ES_HOME=/usr/local/elasticsearch ; 设置ES_HOME 环境变量user=elk ; 启动elasticsearch 的用户directory=/usr/local/elasticsearch ; 进入到这个目录中command=/usr/local/elasticsearch/bin/elasticsearch ; 执行启动命令numprocs=1 ; Supervisor启动这个程序的多个实例,如果numprocs>1,则process_name的表达式必须蕴含%(process_num)s,默认是1autostart=true ; 设置为随 supervisord 启动而启动autorestart=true ; 设置为随 supervisord 重启而重启startretries=3 ; 设置elasticsearch 重启的重试次数priority=1 ; 权重,能够控制程序启动和敞开时的程序,权重越低:越早启动,越晚敞开。默认值是999 ;[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);autorestart=unexpected ; restart at unexpected quit (default: unexpected);startsecs=10 ; number of secs prog must stay running (def. 1);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 b4 SIGKILL (default 10);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 (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 ; # of stderr logfile backups (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 below sample group section shows all possible group values,; create one or more 'real' group: sections to create "heterogeneous"; process groups. ;[group:thegroupname] ; 服务组治理,能够将多个服务名写到这里治理(组名自定义);programs=progname1,progname2 ; 下面配置好的服务名,比方elasticsearch,kibana,logstash;priority=999 ; the relative start priority (default 999) ; 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 = supervisord.d/*.ini
六、自定义配置文件
1、自定义文件寄存地位mkdir /usr/supervisor/supervisord.d/
2、include自定义配置文件
[include]files = /usr/supervisor/supervisord.d/*.conf
3、若须要web查看过程,则去掉[inet_http_server]的正文
[inet_http_server]port=127.0.0.1:9001 ;IP按需配置 username=user password=123
七、运行Supervisor
1、启动supervisord
supervisord -c /usr/supervisor/supervisord.conf
2、建设自定义文件
/usr/supervisor/supervisord.d/ 文件夹中新建 supervisor_test_one.conf 文件
[program:test_one]command=java -jar /data/smallvideo/supervisor/taskApp-exec.jar TaskTestOne ; 被监控的过程门路priority=1 ; 数字越高,优先级越高numprocs=1 ; 启动几个过程autostart=true ; 随着supervisord的启动而启动autorestart=true ; 主动重启startretries=10 ; 启动失败时的最多重试次数exitcodes=0 ; 失常退出代码stopsignal=KILL ; 用来杀死过程的信号stopwaitsecs=10 ; 发送SIGKILL前的等待时间redirect_stderr=true ; 重定向stderr到stdout[program:test_two]command=java -jar /data/smallvideo/supervisor/taskApp-exec.jar TaskTestTwo ; 被监控的过程门路priority=1 ; 数字越高,优先级越高numprocs=1 ; 启动几个过程autostart=true ; 随着supervisord的启动而启动autorestart=true ; 主动重启startretries=10 ; 启动失败时的最多重试次数exitcodes=0 ; 失常退出代码stopsignal=KILL ; 用来杀死过程的信号stopwaitsecs=10 ; 发送SIGKILL前的等待时间redirect_stderr=true ; 重定向stderr到stdout
3、supervisor配置文件详解
- command:启动程序应用的命令,能够是绝对路径或者相对路径- process_name:一个python字符串表达式,用来示意supervisor过程启动的这个的名称,默认值是%(program_name)s- numprocs:Supervisor启动这个程序的多个实例,如果numprocs>1,则process_name的表达式必须蕴含%(process_num)s,默认是1- numprocs_start:一个int偏移值,当启动实例的时候用来计算numprocs的值- priority:权重,能够控制程序启动和敞开时的程序,权重越低:越早启动,越晚敞开。默认值是999- autostart:如果设置为true,当supervisord启动的时候,过程会主动重启。- autorestart:值能够是false、true、unexpected。false:过程不会主动重启,unexpected:当程序退出时的退出码不是exitcodes中定义的时,过程会重启,true:过程会无条件重启当退出的时候。- startsecs:程序启动后期待多长时间后才认为程序启动胜利- startretries:supervisord尝试启动一个程序时尝试的次数。默认是3- exitcodes:一个预期的退出返回码,默认是0,2。- stopsignal:当收到stop申请的时候,发送信号给程序,默认是TERM信号,也能够是 HUP, INT, QUIT, KILL, USR1, or USR2。- stopwaitsecs:在操作系统给supervisord发送SIGCHILD信号时期待的工夫- stopasgroup:如果设置为true,则会使supervisor发送进行信号到整个过程组- killasgroup:如果设置为true,则在给程序发送SIGKILL信号的时候,会发送到整个过程组,它的子过程也会受到影响。- user:如果supervisord以root运行,则会应用这个设置用户启动子程序- redirect_stderr:如果设置为true,过程则会把规范谬误输入到supervisord后盾的规范输入文件描述符。- stdout_logfile:把过程的规范输入写入文件中,如果stdout_logfile没有设置或者设置为AUTO,则supervisor会主动抉择一个文件地位。- stdout_logfile_maxbytes:规范输入log文件达到多少后主动进行轮转,单位是KB、MB、GB。如果设置为0则示意不限度日志文件大小- stdout_logfile_backups:规范输入日志轮转备份的数量,默认是10,如果设置为0,则不备份- stdout_capture_maxbytes:当过程处于stderr capture mode模式的时候,写入FIFO队列的最大bytes值,单位能够是KB、MB、GB- stdout_events_enabled:如果设置为true,当过程在写它的stderr到文件描述符的时候,PROCESS_LOG_STDERR事件会被触发- stderr_logfile:把过程的谬误日志输入一个文件中,除非redirect_stderr参数被设置为true- stderr_logfile_maxbytes:谬误log文件达到多少后主动进行轮转,单位是KB、MB、GB。如果设置为0则示意不限度日志文件大小- stderr_logfile_backups:谬误日志轮转备份的数量,默认是10,如果设置为0,则不备份- stderr_capture_maxbytes:当过程处于stderr capture mode模式的时候,写入FIFO队列的最大bytes值,单位能够是KB、MB、GB- stderr_events_enabled:如果设置为true,当过程在写它的stderr到文件描述符的时候,PROCESS_LOG_STDERR事件会被触发- environment:一个k/v对的list列表- directory:supervisord在生成子过程的时候会切换到该目录- umask:设置过程的umask- serverurl:是否容许子过程和外部的HTTP服务通信,如果设置为AUTO,supervisor会主动的结构一个url
4、运行自定义配置supervisorctl start all
八、supervisor开机主动启动
1、 在目录/usr/lib/systemd/system/ 新建文件supervisord.service,并增加配置内容
[Unit]Description=Process Monitoring and Control DaemonAfter=rc-local.service nss-user-lookup.target[Service]Type=forkingExecStart=/usr/bin/supervisord -c /usr/supervisor/supervisord.conf ;开机启动时执行ExecStop=/usr/bin/supervisord shutdownExecReload=/usr/bin/supervisord reloadkillMode=processRestart=on-failureRestartSec=42s[Install]WantedBy=multi-user.target
2、启动服务systemctl enable supervisord
九、supervisor常用命令
update 更新新的配置到supervisord(不会重启原来已运行的程序)reload,载入所有配置文件,并按新的配置启动、治理所有过程(会重启原来已运行的程序)start xxx: 启动某个过程restart xxx: 重启某个过程stop xxx: 进行某一个过程(xxx),xxx为[program:theprogramname]里配置的值stop groupworker: 重启所有属于名为groupworker这个分组的过程(start,restart同理)stop all,进行全副过程,注:start、restart、stop都不会载入最新的配置文reread,当一个服务由主动启动批改为手动启动时执行一下就ok