Supervisor是一個C/S系統,它容許用戶在類UNIX系統上控制一些進程。它具備如下特性: python
1 簡單 git
Supervisor經過INI格式配置文件進行配置,很容易掌握,它爲每一個進程提供了不少配置選項,可使你很容易的重啓進程或者自動的輪轉日誌。
github
2 統一
web
Supervisor提供了一種統一的方式來start、stop、monitor你的進程, 進程能夠單獨控制,也能夠成組的控制。你能夠在本地或者遠程命令行或者web接口來配置Supervisor。
shell
3 有效
ubuntu
Supervisor經過fork/exec啓動它的子進程,子進程並非守護進程。當一個進程終止的時候,操做系統會當即給Supervisor發送一個信號,而不是像其餘解決方案依賴PID文件。
windows
4 可擴展
安全
Supervisor包含一個簡單的事件通知協議,所以任何程序均可以監控它,並且提供一個XML-RPC控制接口。
app
5 兼容
socket
除了windows平臺,其餘平臺均可運行。
Supervisor系統的組件:
supervisord:
服務會啓動supervisord服務,它負責調用本身啓動子程序,響應來自客戶端的命令,重啓crash或者退出的進程,記錄進程的輸出信息,收集事件信息。該服務的配置文件在/etc/supervisor/supervisord.conf
supervisorctl:
客戶端的命令行工具,提供一個類shell接口,經過它你能夠鏈接到不一樣的supervisord進程上來管理它們各自的子程序。客戶端命令經過UNIX socket或者TCP來和服務通信,服務端能夠要求客戶端提供身份驗證以後才能進行操做([supervisorctl])。
Web Server:
一個小的web接口被集成進了supervisorctl,重啓supervisord以後就能夠訪問了([inet_http_server])。
XML-RPC Interface:
就像HTTP提供WEB UI同樣,同時還提供了XML-RPC接口來控制supervisor和由它運行的程序。
安裝:
supervisor是python編寫的,顯然用easy_install、pip均可以安裝,我懶,直接apt-get了,在ubuntu14.04下安裝完後版本是3.0b2。
Supervisor服務的啓動
其實啓動Supervisor很簡單,supervisord -h看看就知道了,最簡單的-c根配置文件便可:
supervisord -- run a set of applications as daemons. Usage: /usr/bin/supervisord [options] Options: -c/--configuration FILENAME -- configuration file -n/--nodaemon -- run in the foreground (same as 'nodaemon true' in config file) -h/--help -- print this usage message and exit -v/--version -- print supervisord version number and exit -u/--user USER -- run supervisord as this user (or numeric uid) -m/--umask UMASK -- use this umask for daemon subprocess (default is 022) -d/--directory DIRECTORY -- directory to chdir to when daemonized -l/--logfile FILENAME -- use FILENAME as logfile path -y/--logfile_maxbytes BYTES -- use BYTES to limit the max size of logfile -z/--logfile_backups NUM -- number of backups to keep when max bytes reached -e/--loglevel LEVEL -- use LEVEL as log level (debug,info,warn,error,critical) -j/--pidfile FILENAME -- write a pid file for the daemon process to FILENAME -i/--identifier STR -- identifier used for this instance of supervisord -q/--childlogdir DIRECTORY -- the log directory for child process logs -k/--nocleanup -- prevent the process from performing cleanup (removal of old automatic child log files) at startup. -a/--minfds NUM -- the minimum number of file descriptors for start success -t/--strip_ansi -- strip ansi escape codes from process output --minprocs NUM -- the minimum number of processes available for start success --profile_options OPTIONS -- run supervisord under profiler and output results based on OPTIONS, which is a comma-sep'd list of 'cumulative', 'calls', and/or 'callers', e.g. 'cumulative,callers')
不過既然我這懶人是用apt-get安裝的,那安裝包的規範必然符合debian系的風格了,直接service supervisor start便可啓動,且慢,咱們還沒配置supervisor的配置文件呢,啓動了也沒什麼效果。咱們後面詳解配置文件的配置。
supervisorctl客戶端的使用
supervisorctl有兩種模式,一種是交互模式,一種是命令行模式。在命令行輸入supervisorctl直接回車,便可進入交互模式。
supervisorctl -- control applications run by supervisord from the cmd line. Usage: /usr/bin/supervisorctl [options] [action [arguments]] Options: -c/--configuration -- configuration file path (default /etc/supervisor.conf) -h/--help -- print usage message and exit -i/--interactive -- start an interactive shell after executing commands -s/--serverurl URL -- URL on which supervisord server is listening (default "http://localhost:9001"). -u/--username -- username to use for authentication with server -p/--password -- password to use for authentication with server -r/--history-file -- keep a readline history (if readline is available) action [arguments] -- see below Actions are commands like "tail" or "stop". If -i is specified or no action is specified on the command line, a "shell" interpreting actions typed interactively is started. Use the action "help" to find out about available actions.
Supervisor的開機自啓動
若是你是pip或者easy_install安裝的,開機服務自啓動還真是個麻煩事,不過官方已經給出一些rc.d腳本示例了,在github上,不過因爲我是apt-get安裝的,顯然這個開機自啓動是不用擔憂的,用debian的update-rc.d便可搞定。
Supervisor的進程安全
既然是用Supervisor來保證其餘進程的正常運行,可是萬一Supervisor進程掛了怎麼辦,咱們可使用daemontools來保證Supervisor正常運行,就相似於監控的監控。
下篇看配置文件