1.介紹html
Supervisor 是基於 Python 的進程管理工具,只能運行在 Unix-Like 的系統上,也就是沒法運行在 Windows 上。Supervisor 官方版目前只能運行在 Python 2.4 以上版本,可是還沒法運行在 Python 3 上,不過已經有一個 Python 3 的移植版 supervisor-py3k。python
在一個分佈式環境中,每臺機器上可能須要啓動和中止多個進程,使用命令行方式一個一個手動啓動和中止很是麻煩,並且查看每一個進程的狀態也很不方便。若是有一個工具可以實現每臺機器上多個進程的簡單高效中心化管理將是很是方便的。因而Supervisord工具應運而生。linux
supervisor這東西,其實就是用來管理進程的。我們爲何要用supervisor呢?由於,相對於咱們linux傳統的進程管理方式來講,它有不少的優點(簡單,精確,進程組,集中式管理,有效性,可擴展性)。git
Supervisor是一個客戶/服務器系統,它能夠在類Unix系統中管理控制大量進程。Supervisor使用python開發,有多年曆史,目前不少生產環境下的服務器都在使用Supervisor。github
Supervisor的服務器端稱爲supervisord,主要負責在啓動自身時啓動管理的子進程,響應客戶端的命令,重啓崩潰或退出的子進程,記錄子進程stdout和stderr輸出,生成和處理子進程生命週期中的事件。能夠在一個配置文件中配置相關參數,包括Supervisord自身的狀態,其管理的各個子進程的相關屬性。配置文件通常位於/etc/supervisord.conf。web
Supervisor的客戶端稱爲supervisorctl,它提供了一個類shell的接口(即命令行)來使用supervisord服務端提供的功能。經過supervisorctl,用戶能夠鏈接到supervisord服務器進程,得到服務器進程控制的子進程的狀態,啓動和中止子進程,得到正在運行的進程列表。客戶端經過Unix域套接字或者TCP套接字與服務端進行通訊,服務器端具備身份憑證認證機制,能夠有效提高安全性。當客戶端和服務器位於同一臺機器上時,客戶端與服務器共用同一個配置文件/etc/supervisord.conf,經過不一樣標籤來區分二者的配置。shell
2.安裝vim
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