用途
有一个进程需要每时每刻不断的跑,但是这个进程又有可能由于各种原因有可能中断。当进程中断的时候,希望能自动重新启动它。此时,我就需要使用到了Supervisor。
前言
supervisor管理的进程必须由supervisor来启动
supervisor还要求管理的程序是非daemon程序,supervisord会帮你把它转成daemon程序,因此如果用supervisor来管理nginx的话,必须在nginx的配置文件里添加一行设置daemon off让nginx以非daemon方式启动。
supervisor工具包括4个组成部分:supervisord,supervisorctl,Web Server,XML-RPC Interface。
supervisord
它负责在自己的调用启动子程序,响应客户端的命令,重新启动崩溃或退出的子进程,记录其子进程stdout和stderr输出
supervisorctl
supervisord的命令行客户端,是一个shell界面,可以操作supervisord提供的操作功能。从supervisorctl,用户可以连接到不同的supervisord进程,获得每个子进程的状态,以及停止和启动子进程。
它通过UNIX socker或TCP socket和服务端进行连接。
Web Server
在浏览器中执行supervisorctl的各种操作
XML-RPC Interface
和HTTP服务相似,这是提供了一个XML-RPC接口。你可以通过该接口去执行supervisorctl的操作。
其中最重要的是supervisord和supervisorctl。
操作如下:
1、 安装Supervisor
执行以下命令:
yum install python-setuptools
easy_install supervisor
或者
如果easy_install不好使就从官方下载:
wget https://pypi.python.org/packages/80/37/964c0d53cbd328796b1aeb7abea4c0f7b0e8c7197ea9b0b9967b7d004def/supervisor-3.3.1.tar.gz
然后通过python安装:
tar zxf supervisor-3.3.1.tar.gz
cd supervisor
python setup.py install
2、 配置Supervisor
a.创建文件夹和配置文件
mkdir /etc/supervisor
echo_supervisord_conf > /etc/supervisor/supervisord.conf
b.修改/etc/supervisor/supervisord.conf文件内容
; Sample supervisor config file.
;
; For more information on the config file, please see:
; http://supervisord.org/configuration.html
;
; Note: shell expansion ("~" or "$HOME") is not supported. Environment
; variables can be expanded using this syntax: "%(ENV_HOME)s". [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=127.0.0.1: ; (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= ; (num of main logfile rotation backups;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)
;user=root ; (default is current user, required if root)
;password=rootpasswd ; (default is no password (open server))
;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/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= ; 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= ; 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)
;autorestart=unexpected ; whether/when to restart (default: unexpected)
;startsecs= ; number of secs prog must stay running (def. )
;startretries= ; max # of serial start failures (default )
;exitcodes=, ; 'expected' exit codes for process (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 (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 (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 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= ; 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)
;autorestart=unexpected ; whether/when to restart (default: unexpected)
;startsecs= ; number of secs prog must stay running (def. )
;startretries= ; max # of serial start failures (default )
;exitcodes=, ; 'expected' exit codes for process (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 (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 (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 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= ; 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 = conf.d/*.conf [program:printdate]
command=python testvisor.py
directory=/home
autostart=true
autorestart=true
startsecs=3
stdout_logfile=/var/log/supervisor/printdate.log
stdout_logfile_maxbytes=1MB
stdout_logfile_backups=10 [program:redis]
command=redis-server
autostart=true
autorestart=true
startsecs=3
3、 运行supervisord,查看是否生效,执行以下命令:
supervisord -c /etc/supervisor/supervisord.conf
ps -ef | grep ProjectName
返回
root : ? :: redis-server
root : pts/ :: grep --color=auto ProjectName
supervisorctl reload #载入最新的配置文件,停止原有进程,按新的配置文件启动、管理所有进程
supervisorctl update #启动新的配置文件或有改动的进程
supervisorctl status 查看状态
启动全部进程:supervisorctl start all
错误提示:
Unlinking stale socket /tmp/supervisor.sock
unlink /tmp/supervisor.sock
unix:///run/supervisor.sock no such file
重新启动就好
ERROR: [1] bootstrap checks failed
[1]: max file descriptors [65535] for elasticsearch process is too low, increase to at least [65536]
编辑 /etc/security/limits.conf
root soft nofile 65536
root hard nofile 65536
elastic soft nofile 65536
elastic hard nofile 65536