supervisor簡明使用
簡單介紹
supervisor是用Python開發的一套通用的進程管理程序,能將一個普通的命令行進程變爲後臺daemon,並監控進程狀態,異常退出時能自動重啓。它是通過 fork/exec 的方式把這些被管理的進程當作supervisor的子進程來啓動,這樣只要在supervisor的配置文件中,把要管理的進程的可執行文件的路徑寫進去即可。也實現當子進程掛掉的時候,父進程可以準確獲取子進程掛掉的信息的,可以選擇是否自己啓動和報警。
下載安裝
# 需要預先下載 easy_install
easy_install supervisor
配置使用
通過easy_install安裝後,手動生成配置文件:
# 找到 echo_supervisord_conf
find / -name "*supervisord*"
# 找到位置後執行
/usr/bin/supervisord > /etc/supervisord.conf
導入了 supervisor 的配置文件後,創建我們需要用到的環境:
mkdir -p /etc/supervisor/conf.d
以後編寫的需要管理的進程的配置文件就放在 /etc/supervisor/conf.d 目錄下,命名爲:*.conf
,
比如:itkim.conf
。
修改 supervisor 的主配置文件:
[include]
files = /etc/supervisor/conf.d/*.conf # 我們需要管理的進程的配置文件路徑
編寫我們需要管理進程的配置文件,例如:itkim.conf
[program:itkim]
# 腳本目錄
directory=/root/YAB/trading_platform
# 腳本執行命令
command=/usr/bin/python /root/YAB/trading_platform/main.py
# supervisor啓動的時候管理進程隨着同時啓動
autostart=true
# 當管理的進程exit的時候,自動嘗試重啓這個進程
autorestart=true
# 這個選項是子進程啓動多少秒之後,此時狀態如果是running,則我們認爲啓動成功了。
startsecs=1
# 日誌輸出路徑
stderr_logfile=/tmp/blog_stderr.log
stdout_logfile=/tmp/blog_stdout.log
# 腳本運行的用戶身份
user = root
第一次啓動:
supervisord -c /etc/supervisord.conf
配置web界面操作(可選)
通過配置[inet_http_server]可以通過web界面對supervisor控制的進程進行管理
示例:
[inet_http_server]
port=123.56.7.147:8888
username=user
password=passwd
運行管理
supervisorctl status:查看所有進程的狀態
supervisorctl stop 進程名:停止指定進程
supervisorctl stop all:停止所有進程
supervisorctl start 進程名:啓動指定進程
supervisorctl start all:啓動所有進程
supervisorctl restart : 重啓
supervisorctl update :配置文件修改後可以使用該命令加載新的配置
supervisorctl reload: 重新啓動配置中的所有程序
常見錯誤
Exited too quickly (process log may have details).
解決方案:
通過查看 supervisor 的輸出日誌來查找出錯原因。這種情況一般是需要管理的進程代碼哪裏寫的有問題。
# 運行 supervisorctl status 後出現:
supervisor.sock refused connection
解決方案:
這種情況重啓一下 supervisor 試試。
supervisord -c /etc/supervisord.conf
Unlinking stale socket /tmp/supervisor.sock
解決方案:
unlink /tmp/supervisor.sock
Error Another program is already listening on a port that one of our HTTP servers is configured to use
解決方案:
這種情況是我們需要管理的進程監聽的端口號已經被佔用了,關閉目前使用這個端口號的進程即可。(有時可能會出現 ESTABLISHED 狀態)
/usr/lib/python2.7/site-packages/supervisor/options.py:296:UserWarning: Supervisord is running as root and it is searching for its configuration file in default locations (including its current working directory); you probably want to specify a “-c” argument specifying an absolute path to a configuration file for improved security.
解決方案:
supervisord -c /etc/supervisord.conf
附錄
詳細配置:
;[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=1 ; number of processes copies to start (def 1)
;directory=/tmp ; directory to cwd to before exec (def no cwd)
;umask=022 ; umask for process (default None)
;priority=999 ; the relative start priority (default 999)
;autostart=true ; start at supervisord start (default: true)
;startsecs=1 ; # of secs prog must stay up to be running (def. 1)
;startretries=3 ; max # of serial start failures when starting (default 3)
;autorestart=unexpected ; when to restart if exited after running (def: unexpected)
;exitcodes=0,2 ; 'expected' exit codes used with autorestart (default 0,2)
;stopsignal=QUIT ; signal used to kill process (default TERM)
;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
;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=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)
配置說明:
;[program:theprogramname] ;這個就是咱們要管理的子進程了,":"後面的是名字,最好別亂寫和實際進程
有點關聯最好。這樣的program我們可以設置一個或多個,一個program就是
要被管理的一個進程
;command=/bin/cat ; 這個就是我們的要啓動進程的命令路徑了,可以帶參數
例子:/home/test.py -a 'hehe'
有一點需要注意的是,我們的command只能是那種在終端運行的進程,不能是
守護進程。這個想想也知道了,比如說command=service httpd start。
httpd這個進程被linux的service管理了,我們的supervisor再去啓動這個命令
這已經不是嚴格意義的子進程了。
這個是個必須設置的項
;process_name=%(program_name)s ; 這個是進程名,如果我們下面的numprocs參數爲1的話,就不用管這個參數
了,它默認值%(program_name)s也就是上面的那個program冒號後面的名字,
但是如果numprocs爲多個的話,那就不能這麼幹了。想想也知道,不可能每個
進程都用同一個進程名吧。
;numprocs=1 ; 啓動進程的數目。當不爲1時,就是進程池的概念,注意process_name的設置
默認爲1 。。非必須設置
;directory=/tmp ; 進程運行前,會前切換到這個目錄
默認不設置。。。非必須設置
;umask=022 ; 進程掩碼,默認none,非必須
;priority=999 ; 子進程啓動關閉優先級,優先級低的,最先啓動,關閉的時候最後關閉
默認值爲999 。。非必須設置
;autostart=true ; 如果是true的話,子進程將在supervisord啓動後被自動啓動
默認就是true 。。非必須設置
;autorestart=unexpected ; 這個是設置子進程掛掉後自動重啓的情況,有三個選項,false,unexpected
和true。如果爲false的時候,無論什麼情況下,都不會被重新啓動,
如果爲unexpected,只有當進程的退出碼不在下面的exitcodes裏面定義的退
出碼的時候,纔會被自動重啓。當爲true的時候,只要子進程掛掉,將會被無
條件的重啓
;startsecs=1 ; 這個選項是子進程啓動多少秒之後,此時狀態如果是running,則我們認爲啓
動成功了
默認值爲1 。。非必須設置
;startretries=3 ; 當進程啓動失敗後,最大嘗試啓動的次數。。當超過3次後,supervisor將把
此進程的狀態置爲FAIL
默認值爲3 。。非必須設置
;exitcodes=0,2 ; 注意和上面的的autorestart=unexpected對應。。exitcodes裏面的定義的
退出碼是expected的。
;stopsignal=QUIT ; 進程停止信號,可以爲TERM, HUP, INT, QUIT, KILL, USR1, or USR2等信號
默認爲TERM 。。當用設定的信號去幹掉進程,退出碼會被認爲是expected
非必須設置
;stopwaitsecs=10 ; 這個是當我們向子進程發送stopsignal信號後,到系統返回信息
給supervisord,所等待的最大時間。 超過這個時間,supervisord會向該
子進程發送一個強制kill的信號。
默認爲10秒。。非必須設置
;stopasgroup=false ; 這個東西主要用於,supervisord管理的子進程,這個子進程本身還有
子進程。那麼我們如果僅僅幹掉supervisord的子進程的話,子進程的子進程
有可能會變成孤兒進程。所以咱們可以設置可個選項,把整個該子進程的
整個進程組都幹掉。 設置爲true的話,一般killasgroup也會被設置爲true。
需要注意的是,該選項發送的是stop信號
默認爲false。。非必須設置。。
;killasgroup=false ; 這個和上面的stopasgroup類似,不過發送的是kill信號
;user=chrism ; 如果supervisord是root啓動,我們在這裏設置這個非root用戶,可以用來
管理該program
默認不設置。。。非必須設置項
;redirect_stderr=true ; 如果爲true,則stderr的日誌會被寫入stdout日誌文件中
默認爲false,非必須設置
;stdout_logfile=/a/path ; 子進程的stdout的日誌路徑,可以指定路徑,AUTO,none等三個選項。
設置爲none的話,將沒有日誌產生。設置爲AUTO的話,將隨機找一個地方
生成日誌文件,而且當supervisord重新啓動的時候,以前的日誌文件會被
清空。當 redirect_stderr=true的時候,sterr也會寫進這個日誌文件
;stdout_logfile_maxbytes=1MB ; 日誌文件最大大小,和[supervisord]中定義的一樣。默認爲50
;stdout_logfile_backups=10 ; 和[supervisord]定義的一樣。默認10
;stdout_capture_maxbytes=1MB ; 這個東西是設定capture管道的大小,當值不爲0的時候,子進程可以從stdout
發送信息,而supervisor可以根據信息,發送相應的event。
默認爲0,爲0的時候表達關閉管道。。。非必須項
;stdout_events_enabled=false ; 當設置爲ture的時候,當子進程由stdout向文件描述符中寫日誌的時候,將
觸發supervisord發送PROCESS_LOG_STDOUT類型的event
默認爲false。。。非必須設置
;stderr_logfile=/a/path ; 這個東西是設置stderr寫的日誌路徑,當redirect_stderr=true。這個就不用
設置了,設置了也是白搭。因爲它會被寫入stdout_logfile的同一個文件中
默認爲AUTO,也就是隨便找個地存,supervisord重啓被清空。。非必須設置
;stderr_logfile_maxbytes=1MB ; 這個出現好幾次了,就不重複了
;stderr_logfile_backups=10 ; 這個也是
;stderr_capture_maxbytes=1MB ; 這個一樣,和stdout_capture一樣。 默認爲0,關閉狀態
;stderr_events_enabled=false ; 這個也是一樣,默認爲false
;environment=A="1",B="2" ; 這個是該子進程的環境變量,和別的子進程是不共享的
;serverurl=AUTO ;