項目正式部署的時候,我們通常會將其轉化為系統的守護進程,或者使用nohup,screen,&等將其放到後台運行,但是以上方式並不會為我們監控進程的運行狀態,一旦進程崩潰,我們的項目就無法繼續提供服務。
supervisor是一個進程控制系統,由python編寫,它提供了大量的功能來實現對進程的管理
1.程序的多進程啟動,可以配置同時啟動的進程數,而不需要一個個啟動 2.程序的退出碼,可以根據程序的退出碼來判斷是否需要自動重啟 3.程序所產生日志的處理 4.進程初始化的環境,包括目錄,用戶,umask,關閉進程所需要的信號等等 5.手動管理進程(開始,啟動,重啟,查看進程狀態)的web界面,和xmlrpc接口
當然還有其他的一些功能,具體的可以參考官方文檔。
下面說下它的安裝方式:
# 我們可以通過pypi安裝 pip install supervisor # 或者從pypi上下載源碼,然後安裝 python setup.py install # 安裝完之後,使用下列命令來生成配置文件 echo_supervisord_conf > /etc/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] ; supervisord的unix socket服務配置
file=/tmp/supervisor.sock ; socket文件的保存目錄
;chmod=0700 ; socket的文件權限 (default 0700)
;chown=nobody:nogroup ; socket的擁有者和組名
;username=user ; 默認不需要登陸用戶 (open server)
;password=123 ; 默認不需要登陸密碼 (open server)
;[inet_http_server] ; supervisord的tcp服務配置
;port=127.0.0.1:9001 ; tcp端口
;username=user ; tcp登陸用戶
;password=123 ; tcp登陸密碼
[supervisord] ; supervisord的主進程配置
logfile=/tmp/supervisord.log ; 主要的進程日志配置
logfile_maxbytes=50MB ; 最大日志體積,默認50MB
logfile_backups=10 ; 日志文件備份數目,默認10
loglevel=info ; 日志級別,默認info; 還有:debug,warn,trace
pidfile=/tmp/supervisord.pid ; supervisord的pidfile文件
nodaemon=false ; 是否以守護進程的方式啟動
minfds=1024 ; 最小的有效文件描述符,默認1024
minprocs=200 ; 最小的有效進程描述符,默認200
;umask=022 ; 進程文件的umask,默認200
;user=chrism ; 默認為當前用戶,如果為root則必填
;identifier=supervisor ; supervisord的表示符, 默認時'supervisor'
;directory=/tmp ; 默認不cd到當前目錄
;nocleanup=true ; 不在啟動的時候清除臨時文件,默認false
;childlogdir=/tmp ; ('AUTO' child log dir, default $TEMP)
;environment=KEY=value ; 初始鍵值對傳遞給進程
;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:///tmp/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 ; 如果設置應該與http_username相同
;password=123 ; 如果設置應該與http_password相同
;prompt=mysupervisor ; 命令行提示符,默認"supervisor"
;history_file=~/.sc_history ; 命令行歷史紀錄
; 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 ; 運行的程序 (相對使用PATH路徑, 可以使用參數)
;process_name=%(program_name)s ; 進程名表達式,默認為%(program_name)s
;numprocs=1 ; 默認啟動的進程數目,默認為1
;directory=/tmp ; 在運行前cwd到指定的目錄,默認不執行cmd
;umask=022 ; 進程umask,默認None
;priority=999 ; 程序運行的優先級,默認999
;autostart=true ; 默認隨supervisord自動啟動,默認true
;autorestart=unexpected ; whether/when to restart (default: unexpected)
;startsecs=1 ; number of secs prog must stay running (def. 1)
;startretries=3 ; max # of serial start failures (default 3)
;exitcodes=0,2 ; 期望的退出碼,默認0,2
;stopsignal=QUIT ; 殺死進程的信號,默認TERM
;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false ; 向unix進程組發送停止信號,默認false
;killasgroup=false ; 向unix進程組發送SIGKILL信號,默認false
;user=chrism ; 為運行程序的unix帳號設置setuid
;redirect_stderr=true ; 將標准錯誤重定向到標准輸出,默認false
;stdout_logfile=/a/path ; 標准輸出的文件路徑NONE=none;默認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 ; 運行的程序 (相對使用PATH路徑, 可以使用參數)
;process_name=%(program_name)s ; 進程名表達式,默認為%(program_name)s
;numprocs=1 ; 默認啟動的進程數目,默認為1
;events=EVENT ; event notif. types to subscribe to (req'd)
;buffer_size=10 ; 事件緩沖區隊列大小,默認10
;directory=/tmp ; 在運行前cwd到指定的目錄,默認不執行cmd
;umask=022 ; 進程umask,默認None
;priority=-1 ; 程序運行的優先級,默認-1
;autostart=true ; 默認隨supervisord自動啟動,默認true
;autorestart=unexpected ; whether/when to restart (default: unexpected)
;startsecs=1 ; number of secs prog must stay running (def. 1)
;startretries=3 ; max # of serial start failures (default 3)
;exitcodes=0,2 ; 期望的退出碼,默認0,2
;stopsignal=QUIT ; 殺死進程的信號,默認TERM
;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false ; 向unix進程組發送停止信號,默認false
;killasgroup=false ; 向unix進程組發送SIGKILL信號,默認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=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 ; 任何在[program:x]中定義的x
;priority=999 ; 程序運行的優先級,默認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 = relative/directory/*.ini