今天在學習的時候痕钢,涉及到了隊列監(jiān)聽的問題图柏,然后老師介紹了一個工具。以為是什么常見的東西任连,百度了下發(fā)現真是個好東西蚤吹。
?項目中遇到有些腳本需要通過后臺進程運行,保證不被異常中斷随抠,之前都是通過nohup裁着、&、screen來實現拱她,帶著能否做一個start/stop/restart/reload的服務啟動的想法找到里Supervisor二驰。關于supervisor的介紹在網上大致搜索總結如下:
? ? ? Supervisor是用Python開發(fā)的一套通用的進程管理程序,能將一個普通的命令行進程變?yōu)楹笈_daemon秉沼,并監(jiān)控進程狀態(tài)桶雀,異常退出時能自動重啟。它是通過fork/exec的方式把這些被管理的進程當作supervisor的子進程來啟動唬复,這樣只要在supervisor的配置文件中矗积,把要管理的進程的可執(zhí)行文件的路徑寫進去即可。也實現當子進程掛掉的時候敞咧,父進程可以準確獲取子進程掛掉的信息的棘捣,可以選擇是否自己啟動和報警。supervisor還提供了一個功能休建,可以為supervisord或者每個子進程柱锹,設置一個非root的user,這個user就可以管理它對應的進程丰包。
1:easy_install 安裝:
easy_install supervisor2:pip 安裝:
pip install supervisor3:Debian / Ubuntu可以直接通過apt安裝:
apt-get install supervisor
1)通過apt-get install安裝后,supervisor的配置文件在:
/etc/supervisor/supervisord.conf?
supervisor的配置文件默認是不全的邑彪,不過在大部分默認的情況下瞧毙,上面說的基本功能已經滿足。而其管理的子進程配置文件在:
/etc/supervisor/conf.d/*.conf
然后寄症,開始給自己需要的腳本程序編寫一個子進程配置文件宙彪,讓supervisor來管理它,放在/etc/supervisor/conf.d/目錄下有巧,以.conf作為擴展名(每個進程的配置文件都可以單獨分拆也可以把相關的腳本放一起)释漆。如任意定義一個和腳本相關的項目名稱的選項組(/etc/supervisor/conf.d/test.conf):
#項目名
[program:blog]
#腳本目錄
directory=/opt/bin
#腳本執(zhí)行命令
command=/usr/bin/python /opt/bin/test.py
#supervisor啟動的時候是否隨著同時啟動,默認True
autostart=true
#當程序exit的時候篮迎,這個program不會自動重啟,默認unexpected
#設置子進程掛掉后自動重啟的情況男图,有三個選項示姿,false,unexpected和true。如果為false的時候逊笆,無論什么情況下栈戳,都不會被重新啟動,如果為unexpected难裆,只有當進程的退出碼不在下面的exitcodes里面定義的
autorestart=false
#這個選項是子進程啟動多少秒之后子檀,此時狀態(tài)如果是running,則我們認為啟動成功了乃戈。默認值為1
startsecs=1
#日志輸出
stderr_logfile=/tmp/blog_stderr.log
stdout_logfile=/tmp/blog_stdout.log
#腳本運行的用戶身份
user = zhoujy
#把 stderr 重定向到 stdout褂痰,默認 false
redirect_stderr = true
#stdout 日志文件大小,默認 50MB
stdout_logfile_maxbytes = 20M
#stdout 日志文件備份數
stdout_logfile_backups = 20
[program:zhoujy] #說明同上
directory=/opt/bin
command=/usr/bin/python /opt/bin/zhoujy.py
autostart=true
autorestart=false
stderr_logfile=/tmp/zhoujy_stderr.log
stdout_logfile=/tmp/zhoujy_stdout.log
#user = zhoujy
通過easy_install安裝后症虑,配置文件不存在缩歪,需要自己導入。
①:運行echo_supervisord_conf打印出一個配置文件的樣本侦讨,樣本說明可以看supervisor(一)基礎篇的詳細說明驶冒,要是設置樣本為一個配置文件則:
1:運行 echo_supervisord_conf,查看配置樣本:
echo_supervisord_conf2:創(chuàng)建配置文件:
echo_supervisord_conf > /etc/supervisord.conf
②:配置子進程配置文件韵卤,可以直接在supervisor中的;[program:theprogramname]里設置骗污。
詳細的子進程配置文件:
樣本:
;[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? ? ? ? ? ? ? ? ? ; 子進程啟動關閉優(yōu)先級劣像,優(yōu)先級低的乡话,最先啟動,關閉的時候最后關閉
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 默認值為999 耳奕。绑青。非必須設置
;autostart=true? ? ? ? ? ? ? ? ; 如果是true的話,子進程將在supervisord啟動后被自動啟動
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 默認就是true? 屋群。闸婴。非必須設置
;autorestart=unexpected? ? ? ? ; 這個是設置子進程掛掉后自動重啟的情況,有三個選項芍躏,false,unexpected
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 和true邪乍。如果為false的時候,無論什么情況下对竣,都不會被重新啟動庇楞,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 如果為unexpected,只有當進程的退出碼不在下面的exitcodes里面定義的退
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 出碼的時候否纬,才會被自動重啟吕晌。當為true的時候,只要子進程掛掉临燃,將會被無
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 條件的重啟
;startsecs=1? ? ? ? ? ? ? ? ? ; 這個選項是子進程啟動多少秒之后睛驳,此時狀態(tài)如果是running,則我們認為啟
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 動成功了
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 默認值為1 谬俄。柏靶。非必須設置
;startretries=3? ? ? ? ? ? ? ? ; 當進程啟動失敗后,最大嘗試啟動的次數溃论。屎蜓。當超過3次后,supervisor將把
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 此進程的狀態(tài)置為FAIL
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 默認值為3 钥勋。炬转。非必須設置
;exitcodes=0,2? ? ? ? ? ? ? ? ; 注意和上面的的autorestart=unexpected對應辆苔。。exitcodes里面的定義的
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 退出碼是expected的扼劈。
;stopsignal=QUIT? ? ? ? ? ? ? ; 進程停止信號驻啤,可以為TERM, HUP, INT, QUIT, KILL, USR1, or USR2等信號
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 默認為TERM 。荐吵。當用設定的信號去干掉進程先紫,退出碼會被認為是expected
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 非必須設置
;stopwaitsecs=10? ? ? ? ? ? ? ; 這個是當我們向子進程發(fā)送stopsignal信號后,到系統返回信息
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 給supervisord氯迂,所等待的最大時間茅逮。 超過這個時間,supervisord會向該
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 子進程發(fā)送一個強制kill的信號薯蝎。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 默認為10秒遥倦。。非必須設置
;stopasgroup=false? ? ? ? ? ? ; 這個東西主要用于占锯,supervisord管理的子進程袒哥,這個子進程本身還有
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 子進程。那么我們如果僅僅干掉supervisord的子進程的話消略,子進程的子進程
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 有可能會變成孤兒進程堡称。所以咱們可以設置可個選項,把整個該子進程的
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 整個進程組都干掉疑俭。 設置為true的話粮呢,一般killasgroup也會被設置為true。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 需要注意的是钞艇,該選項發(fā)送的是stop信號
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 默認為false啄寡。。非必須設置哩照。挺物。
;killasgroup=false? ? ? ? ? ? ; 這個和上面的stopasgroup類似,不過發(fā)送的是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
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 發(fā)送信息,而supervisor可以根據信息脐嫂,發(fā)送相應的event统刮。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 默認為0,為0的時候表達關閉管道账千。侥蒙。。非必須項
;stdout_events_enabled=false? ; 當設置為ture的時候匀奏,當子進程由stdout向文件描述符中寫日志的時候鞭衩,將
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 觸發(fā)supervisord發(fā)送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撤摸,關閉狀態(tài)
;stderr_events_enabled=false? ; 這個也是一樣,默認為false
;environment=A="1",B="2"? ? ? ; 這個是該子進程的環(huán)境變量,和別的子進程是不共享的
;serverurl=AUTO? ? ? ? ? ? ?
改成自己實際的配置文件:和①上面一樣愁溜。
1)apt-get install 安裝的supervisor直接可以通過/etc/init.d/supervisor 運行:
/etc/init.d/supervisor start
2)通過easy_install 安裝的supervisor運行supervisord 運行:
supervisord
4:web界面操作
需要在supervisor的配置文件里添加[inet_http_server]選項組:之后可以通過http://10.211.55.11:9001來訪問控制子線程的管理疾嗅。
[inet_http_server]
port=10.211.55.11:9001username=user
password=123
效果:
5:子進程管理(supervisorctl)
1) 查看所有子進程的狀態(tài):
#supervisorctlstatusblog? ? ? ? ? ? ? ? ? ? ? ? ? ? RUNNING? ? pid2395,uptime0:08:41zhoujy? ? ? ? ? ? ? ? ? ? ? ? ? RUNNING? ? pid 2396,uptime0:08:41
2)關閉、開啟指定的子進程:
#supervisorctlstopzhoujyzhoujy: stopped#supervisorctlstartzhoujyzhoujy: started
3)關閉冕象、開啟所有的子進程:
#supervisorctlstop allblog: stoppedzhoujy: stopped#supervisorctlstart allblog: startedzhoujy: started
4):其他參數:supervisor開啟后子進程自動開啟(autostart=true)和子進程退出后自動啟動(autorestart=ture)
更多的參數可以看官方文檔和supervisor(一)基礎篇的說明代承。
總結:
?我們需要的功能在上面介紹的supervisor的基本功能中已經實現,supervisor還有其他的一些功能:如進程組渐扮、web頁面管理子進程论悴、監(jiān)控子線程情況等等,更多的信息可以去官網上查看墓律。
參考文檔:
?? ;這個東西不錯膀估,回頭再用到的時候要好好再對照這個文章來練習一下。