1.介绍

Supervisor 是基于 Python 的进程管理工具,只能运行在 Unix-Like 的系统上,也就是无法运行在 Windows 上。Supervisor 官方版目前只能运行在 Python 2.4 以上版本,但是还无法运行在 Python 3 上,不过已经有一个 Python 3 的移植版 supervisor-py3k

在一个分布式环境中,每台机器上可能需要启动和停止多个进程,使用命令行方式一个一个手动启动和停止非常麻烦,而且查看每个进程的状态也很不方便。如果有一个工具能够实现每台机器上多个进程的简单高效中心化管理将是非常方便的。于是Supervisord工具应运而生。

supervisor这东西,其实就是用来管理进程的。咱们为什么要用supervisor呢?因为,相对于我们linux传统的进程管理方式来说,它有很多的优势(简单,精确,进程组,集中式管理,有效性,可扩展性)。

 

Supervisor是一个客户/服务器系统,它可以在类Unix系统中管理控制大量进程。Supervisor使用python开发,有多年历史,目前很多生产环境下的服务器都在使用Supervisor。

      Supervisor的服务器端称为supervisord,主要负责在启动自身时启动管理的子进程,响应客户端的命令,重启崩溃或退出的子进程,记录子进程stdout和stderr输出,生成和处理子进程生命周期中的事件。可以在一个配置文件中配置相关参数,包括Supervisord自身的状态,其管理的各个子进程的相关属性。配置文件一般位于/etc/supervisord.conf。

      Supervisor的客户端称为supervisorctl,它提供了一个类shell的接口(即命令行)来使用supervisord服务端提供的功能。通过supervisorctl,用户可以连接到supervisord服务器进程,获得服务器进程控制的子进程的状态,启动和停止子进程,获得正在运行的进程列表。客户端通过Unix域套接字或者TCP套接字与服务端进行通信,服务器端具有身份凭证认证机制,可以有效提升安全性。当客户端和服务器位于同一台机器上时,客户端与服务器共用同一个配置文件/etc/supervisord.conf,通过不同标签来区分两者的配置。

 

2.安装

2.1

2.2



$ sudo apt-get install python-pip python-dev build-essential 
$ sudo pip install supervisor

$ sudo mkdir /usr/local/supervisor/
$ sudo echo_supervisord_conf > /usr/local/supervisor/supervisord.conf


$ sudo vim /usr/local/supervisor/supervisord.conf


1 ; Sample supervisor config file.
 2 ;
 3 ; For more information on the config file, please see:
 4 ; http://supervisord.org/configuration.html
 5 ;
 6 ; Notes:
 7 ; - Shell expansion ("~" or "$HOME") is not supported. Environment
 8 ; variables can be expanded using this syntax: "%(ENV_HOME)s".
 9 ; - Comments must have a leading space: "a=b ;comment" not "a=b;comment".
 10 
 11 [unix_http_server]
 12 file=/tmp/supervisor.sock ; (the path to the socket file)            ; socket文件的路径,supervisorctl用XML_RPC和supervisord通信就是通过它进行的。如果不设置的话,supervisorctl也就不能用了 13 ;chmod=0700 ; socket file mode (default 0700)
 14 ;chown=nobody:nogroup ; socket file uid:gid owner
 15 ;username=user ; (default is no username (open server))
 16 ;password=123 ; (default is no password (open server))
 17 
 18 [inet_http_server] ; inet (TCP) server disabled by default
 19 port=0.0.0.0:9001 ; (ip_address:port specifier, *:port for all iface)
 20 ;username=patpat ; (default is no username (open server))
 21 ;password=patpat ; (default is no password (open server))
 22 
 23 [supervisord]
 24 logfile=/tmp/supervisord.log ; (main log file;default $CWD/supervisord.log)
 25 logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
 26 logfile_backups=10 ; (num of main logfile rotation backups;default 10)
 27 loglevel=info ; (log level;default info; others: debug,warn,trace)
 28 pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
 29 nodaemon=false ; (start in foreground if true;default false)
 30 minfds=1024 ; (min. avail startup file descriptors;default 1024)
 31 minprocs=200 ; (min. avail process descriptors;default 200)
 32 ;umask=022 ; (process file creation umask;default 022)
 33 ;user=chrism ; (default is current user, required if root)
 34 ;identifier=supervisor ; (supervisord identifier, default is 'supervisor')
 35 ;directory=/tmp ; (default is not to cd during start)
 36 ;nocleanup=true ; (don't clean up tempfiles at start;default false)
 37 ;childlogdir=/tmp ; ('AUTO' child log dir, default $TEMP)
 38 ;environment=KEY="value" ; (key value pairs to add to environment)
 39 ;strip_ansi=false ; (strip ansi escape codes in logs; def. false)
 40 
 41 ; the below section must remain in the config file for RPC
 42 ; (supervisorctl/web interface) to work, additional interfaces may be
 43 ; added by defining them in separate rpcinterface: sections
 44 [rpcinterface:supervisor]
 45 supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
 46 
 47 [supervisorctl]
 48 serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket
 49 ;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
 50 ;username=chris ; should be same as http_username if set
 51 ;password=123 ; should be same as http_password if set
 52 ;prompt=mysupervisor ; cmd line prompt (default "supervisor")
 53 ;history_file=~/.sc_history ; use readline history if available
 54 
 55 ; The below sample program section shows all possible program subsection values,
 56 ; create one or more 'real' program: sections to be able to control them under
 57 ; supervisor.
 58 
 59 ;[program:theprogramname]
 60 ;command=/bin/cat ; the program (relative uses PATH, can take args)
 61 ;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
 62 ;numprocs=1 ; number of processes copies to start (def 1)
 63 ;directory=/tmp ; directory to cwd to before exec (def no cwd)
 64 ;umask=022 ; umask for process (default None)
 65 ;priority=999 ; the relative start priority (default 999)
 66 ;autostart=true ; start at supervisord start (default: true)
 67 ;startsecs=1 ; # of secs prog must stay up to be running (def. 1)
 68 ;startretries=3 ; max # of serial start failures when starting (default 3)
 69 ;autorestart=unexpected ; when to restart if exited after running (def: unexpected)
 70 ;exitcodes=0,2 ; 'expected' exit codes used with autorestart (default 0,2)
 71 ;stopsignal=QUIT ; signal used to kill process (default TERM)
 72 ;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
 73 ;stopasgroup=false ; send stop signal to the UNIX process group (default false)
 74 ;killasgroup=false ; SIGKILL the UNIX process group (def false)
 75 ;user=chrism ; setuid to this UNIX account to run the program
 76 ;redirect_stderr=true ; redirect proc stderr to stdout (default false)
 77 ;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO
 78 ;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
 79 ;stdout_logfile_backups=10 ; # of stdout logfile backups (default 10)
 80 ;stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
 81 ;stdout_events_enabled=false ; emit events on stdout writes (default false)
 82 ;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO
 83 ;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
 84 ;stderr_logfile_backups=10 ; # of stderr logfile backups (default 10)
 85 ;stderr_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
 86 ;stderr_events_enabled=false ; emit events on stderr writes (default false)
 87 ;environment=A="1",B="2" ; process environment additions (def no adds)
 88 ;serverurl=AUTO ; override serverurl computation (childutils)
 89 
 90 ; The below sample eventlistener section shows all possible
 91 ; eventlistener subsection values, create one or more 'real'
 92 ; eventlistener: sections to be able to handle event notifications
 93 ; sent by supervisor.
 94 
 95 ;[eventlistener:theeventlistenername]
 96 ;command=/bin/eventlistener ; the program (relative uses PATH, can take args)
 97 ;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
 98 ;numprocs=1 ; number of processes copies to start (def 1)
 99 ;events=EVENT ; event notif. types to subscribe to (req'd)
100 ;buffer_size=10 ; event buffer queue size (default 10)
101 ;directory=/tmp ; directory to cwd to before exec (def no cwd)
102 ;umask=022 ; umask for process (default None)
103 ;priority=-1 ; the relative start priority (default -1)
104 ;autostart=true ; start at supervisord start (default: true)
105 ;startsecs=1 ; # of secs prog must stay up to be running (def. 1)
106 ;startretries=3 ; max # of serial start failures when starting (default 3)
107 ;autorestart=unexpected ; autorestart if exited after running (def: unexpected)
108 ;exitcodes=0,2 ; 'expected' exit codes used with autorestart (default 0,2)
109 ;stopsignal=QUIT ; signal used to kill process (default TERM)
110 ;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
111 ;stopasgroup=false ; send stop signal to the UNIX process group (default false)
112 ;killasgroup=false ; SIGKILL the UNIX process group (def false)
113 ;user=chrism ; setuid to this UNIX account to run the program
114 ;redirect_stderr=false ; redirect_stderr=true is not allowed for eventlisteners
115 ;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO
116 ;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
117 ;stdout_logfile_backups=10 ; # of stdout logfile backups (default 10)
118 ;stdout_events_enabled=false ; emit events on stdout writes (default false)
119 ;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO
120 ;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
121 ;stderr_logfile_backups=10 ; # of stderr logfile backups (default 10)
122 ;stderr_events_enabled=false ; emit events on stderr writes (default false)
123 ;environment=A="1",B="2" ; process environment additions
124 ;serverurl=AUTO ; override serverurl computation (childutils)
125 
126 ; The below sample group section shows all possible group values,
127 ; create one or more 'real' group: sections to create "heterogeneous"
128 ; process groups.
129 
130 ;[group:thegroupname]
131 ;programs=progname1,progname2 ; each refers to 'x' in [program:x] definitions
132 ;priority=999 ; the relative start priority (default 999)
133 
134 ; The [include] section can just contain the "files" setting. This
135 ; setting can list multiple files (separated by whitespace or
136 ; newlines). It can also contain wildcards. The filenames are
137 ; interpreted as relative to this file. Included files *cannot*
138 ; include files themselves.
139 
140 [include]
141 files = conf.d/*.conf


$ sudo supervisord -c /usr/local/supervisor/supervisord.conf