1.Supervisor 工具作用?
Supervisor 是用 Python 开发的一套通用的过程管理程序,能将一个一般的命令行过程变为后盾 daemon,并监控过程状态,异样退出时能主动重启。
看之前先明确 Supervisor 有啥用,到底能帮到咱们什么呢?
1. 能够将一些脚本设置为以守护过程的形式始终在后盾运行。
2. 监控过程的运行状况,面对突发状况能及时预警或者重启,防止出现过程挂掉,影响业务的失常运行
2. 装置
装置形式:
1.pip 装置(需装置有 pip)pip install supervisor
2.yum 装置 配置好 yum 源。yum install -y supervisor
装置 supervisor
提醒找不到 supervisor 时,须要 yum install epel-release 先装置 EPEL 源)
装置好应用 supervisord - v 查看版本。
此处应用 yum 装置的 3.4 版本
3. 应用
执行
echo_supervisord_conf > /etc/supervisord.conf
创立一个官网默认的配置文件 (supervisord.conf 主配置文件) 到 etc 目录下。
能够本人依据爱好批改配置文件的设置。
我本人批改了一些配置文件门路,日志门路等。可供大家参考
;Sample supervisor config file.
[unix_http_server]
file=/var/supervisor/run/supervisor.sock ; (the path to the socket file)
;chmod=0700 ; sockef 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] ; 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))
[supervisord]
logfile=/var/supervisor/log/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=/var/supervisor/run/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)
;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 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/supervisor/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 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]
;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)
;autorestart=true ; retstart at unexpected quit (default: true)
;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_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 (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 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.
;[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 ; each refers to 'x' in [program:x] definitions
;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
files = /var/supervisor/conf/*.conf
我的配置文件中,须要在 /var/supervisor 中创立 3 个文件夹,别离是 conf,log,run.
run 文件夹下有 supervisord.pid 和 supervisor.sock
conf 文件夹放配置文件(次配置文件寄存目录)。
log 文件夹放日志文件。
这样能够对立治理 superviosr.
改好配置文件后。
如果启动呢?
装置 superviosr 会主动在 /usr/bin 目录下生成两个命令
应用 supervisord 命令即可启动
supervisord -c /etc/supervisord.conf
而后去 /var/supervisor/conf 目录下,增加对应的子过程配置文件。我的子配置文件如下, 写了具体的正文,能够依据本人需要批改对应的配置我的项目。
[program:time] ; 项目名称
directory = /home/wwwroor/test/public ; 程序的启动目录,我的项目根目录的上一级
command = /usr/local/php/bin/php index.php api/task/test ; 启动命令
process_name=%(program_name)s_%(process_num)02d
numprocs = 1 ; 开启的过程数量
autostart = true ; 在 supervisord 启动的时候也主动启动
startsecs = 15 ; 启动 5 秒后没有异样退出,就当作曾经失常启动了
autorestart = true ; 程序异样退出后主动重启
startretries = 300 ; 启动失败主动重试次数,默认是 3
user = root ; 用哪个用户启动
redirect_stderr = true ; 把 stderr 重定向到 stdout,默认 false
stdout_logfile_maxbytes = 50MB ; stdout 日志文件大小,默认 50MB
stdout_logfile_backups = 20 ; stdout 日志文件备份数
; stdout 日志文件, 须要手动创立目录(supervisord 会主动创立日志文件)stdout_logfile = /var/supervisor/log/test.log
loglevel=info
其中次要的就是 command 配置,写的须要变成守护过程的命令。
而后从新加载配置
$ supervisorctl reread
$ supervisorctl update
查看子过程运行状况supervisorctl status
这样就某些脚本当前守护过程的形式跑起来了,并且监控了它。
好啦,这样就有了监控的机制,我的项目的运行就稳固多啦,先分享到这,当前有新的教训在写啦。
本文为夜雨闻铃原创文章, 转载无需和我分割, 但请注明文章出处。文章出处:夜雨闻铃的思否文章 https://segmentfault.com/a/11…