Supervisor:實(shí)現(xiàn)process持久運(yùn)行

前段時(shí)間,口袋需要對(duì)一個(gè)600w的MySQL表進(jìn)行處理,數(shù)據(jù)處理使用的語(yǔ)言是Python。

代碼部署在192.54服務(wù)器上,通過(guò)SSH登陸到遠(yuǎn)程服務(wù)器后,手動(dòng)執(zhí)行該任務(wù)。由于涉及到的數(shù)據(jù)量比較大,會(huì)比較耗時(shí),直接運(yùn)行 python test.py 的話,中途可能會(huì)因?yàn)殛P(guān)閉 Terminal、網(wǎng)絡(luò)不穩(wěn)定等各種原因,導(dǎo)致進(jìn)程中斷。

為保證服務(wù)能盡快圓滿(mǎn)執(zhí)行,需要時(shí)刻關(guān)注進(jìn)程狀態(tài),出現(xiàn)故障時(shí),及時(shí)手動(dòng)重啟。顯而易見(jiàn),這樣會(huì)浪費(fèi)大量的精力 & 打斷正常工作節(jié)奏。

如何讓命令提交后不受本地關(guān)閉終端窗口/網(wǎng)絡(luò)斷開(kāi)連接等因素的干擾呢?接下來(lái)找解決方案:

  • nohup
  • supervisor

nohup

nohup,即 no hangup,nohup 的用途就是讓提交的命令忽略 hangup 信號(hào),從而使我們的進(jìn)程避免中途被中斷。

使用方法也很簡(jiǎn)單,直接在命令前加上nohup即可,標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯(cuò)誤缺省會(huì)被重定向到 nohup.out 文件中。一般我們可在結(jié)尾加上&來(lái)將命令同時(shí)放入后臺(tái)運(yùn)行,也可用">filename 2>&1"來(lái)更改默認(rèn)的重定向文件名。

nohup python update_t_drug_readnum.py &

考慮到nohup簡(jiǎn)單好用,開(kāi)始時(shí),我也嘗試使用nohup & 來(lái)執(zhí)行,但是仍然會(huì)莫名其妙的中斷,窩火ing,無(wú)奈下繼續(xù)尋找新方法,接著找到linux中的一個(gè)神器:supervisor

supervisor

Supervisor is a client/server system that allows its users to monitor and control a number of processes on UNIX-like operating systems. -- from http://supervisord.org/
說(shuō)人話就是:supervisor是用Python開(kāi)發(fā)的一套client/server(是不是在學(xué)docker時(shí),也遇到過(guò)Docker Engine is a client-server application)進(jìn)程管理程序,能讓用戶(hù)在類(lèi)Linux操作系統(tǒng)中監(jiān)控一組進(jìn)程。

components

supervisord

supervisor 的服務(wù)端叫做supervisord。它負(fù)責(zé)啟動(dòng)子進(jìn)程(將普通的命令行進(jìn)程變?yōu)楹笈_(tái)daemon)、響應(yīng)客戶(hù)端命令、監(jiān)控進(jìn)程狀態(tài)、自動(dòng)重啟crashed掉的子進(jìn)程、紀(jì)錄子進(jìn)程的stdout、stderr。

supervisord需要用戶(hù)在配置文件:/etc/supervisord.conf中配置一些滿(mǎn)足業(yè)務(wù)需要的自定義參數(shù)。

supervisorctl

supervisor 的客戶(hù)端是supervisorctl,它提供了shell-like 接口來(lái)調(diào)用supervisord。通過(guò)supervisorctl,用戶(hù)可以連接到多個(gè)supervisord進(jìn)程,獲取supervisord下的子進(jìn)程狀態(tài),stop or start 子進(jìn)程,獲取supervisord中running狀態(tài)的子進(jìn)程。

supervisorctl 與 supervisord的交互是通過(guò)TCP協(xié)議。

Web Server

同時(shí) supervisor 也提供了web界面來(lái)訪問(wèn) supervisord

XML-RPC Interface

supervisor 也提供了XML-RPC接口來(lái)訪問(wèn) supervisord

使用步驟

  • 安裝supervisor: pip install supervisor
  • 生成默認(rèn)配置文件
echo_supervisord_conf > /etc/supervisord.conf
  • 修改/etc/supervisord.conf
[program:drug]0
command=python update_t_drug_readnum.py
autostart=true
  • 啟動(dòng)supervisord
supervisord -c /etc/supervisord.conf
supervisord // 也可直接運(yùn)行該命令,默認(rèn)找/etc/supervisord.conf的配置文件
  • 啟動(dòng)supervosirctl
supervisorctl
  • 之后即可以在shell中操作 supervisord 管理的進(jìn)程了

其他常見(jiàn)的命令有:

  • supervisorctl status
  • supervisorctl start appname
  • supervisorctl restart appname
  • supervisorctl stop appname
  • supervisorctl stop all
  • supervisorctl reload

如果修改了 /etc/supervisord.conf ,需要執(zhí)行 supervisorctl reload 來(lái)重新加載配置文件,否則不會(huì)生效...

除了官網(wǎng)后,還有一篇blog也不錯(cuò):Monitoring Processes with Supervisord

可以看到,supervisor上手是非常簡(jiǎn)單易用的,雖然目前我只使用了supervisor的監(jiān)控子進(jìn)程 & 自動(dòng)重啟的功能,但也足以保證這次數(shù)據(jù)處理過(guò)程的平穩(wěn)運(yùn)行。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容