c++ createtoolhelp32snapshot取程序路徑_Supervisord管理程序實踐
技術標籤:c++ createtoolhelp32snapshot取程序路徑javamac系統通過pid獲取程序名稱kill 程序kill程序supervisord [supervisorctl]
今天湊空研究了下Supervisord,這是一款linux程序管理工具,使用python開發,主要用於在後臺維護程序(類似master守護程序),可以實現監控程序的狀態、自動重啟程序等操作,便於一些服務的維護與監控。
安裝Supervisord
由於是用python開發的,因此使用pip安裝最為方便。
|
|
說明:安裝完成之後多了3個工具:echo_supervisord_conf、supervisorctl和supervisord。
Supervisord配置檔案
首先可以使用echo_supervisord_conf命令獲取supervisor配置模板:
|
|
說明:該命令在當前目錄下建立了一個檔名為supervisord.conf的配置檔案,編輯配置檔案:
|
|
來看看預設配置檔案中的主要配置項:(還有一些配置不常用,可以忽略)
|
|
執行以下命令啟動supervisord程序,可測試supervisord是否安裝成功並執行。
|
|
檢視系統程序中是否多了一個supervisord:
|
|
配置Program
program就是用來配置監控不同的應用程式程序的,推薦每個應用程式單獨寫一個program配置檔案,然後在supervisord.conf中通過include載入所有應用程式的配置。
這裡拿建立一個celery程序為例,首先在supervisord.conf最後一行寫入:
|
|
然後建立/etc/supervisor目錄,併到目錄下建立/etc/supervisor/celery_touchscan.conf檔案,寫入:
|
|
重啟supervisord程序:
|
|
此時檢視系統上的程序,發現建立了一個supervisord守護程序,10個celery的work程序(celery的work程序數量取決於command命令中的-c引數以及配置檔案中的numprocs引數,numprocs引數是指執行幾次command命令,而在celery命令列中指定了需要執行的work數量)
說明:此時如果手動kill掉celery的work程序,會發現celery的work程序會被supervisord自動重啟,只有當supervisord守護程序被kill以後,才能真正kill掉celery的work程序。
supervisord命令列操作
啟動supervisord程序
1 |
|
關閉supervisord程序
|
|
重啟supervisord程序
|
|
檢視程序狀態
|
|
效果如下:
每列分別代表:programe名稱、程序名稱,程序狀態、程序id,執行時間
更多supervisorctl命令
|
|
說明:可以直接在系統shell中執行,也可以先執行supervisorctl,進入supervisorctl_shell中執行相應的命令。
針對Python環境
如果專案使用了python的pyenv模組來設定環境,則supervisord配置檔案中需要指定python環境的路徑。其中有兩種方式指定程式使用的Python環境:
command使用絕對路徑。
通過environment配置PYTHONPATH。
使用supervisord注意點
子程序問題
有時候用Supervisor託管的程式還會有子程序,如果只殺死主程序,子程序就可能變成孤兒程序。通過以下這兩項配置來確保所有子程序都能正確停止:
|
|
配置更新
每次修改supervisord配置檔案後,需要重啟supervisord程序。
後臺程式問題
Supervisor只能管理在前臺執行的程式,所以如果應用程式有後臺執行的選項,需要關閉。
supervisord與定時任務
supervisord主要用來管理程序,而不是排程任務,因此如果有定時任務的需求,跟結合crontab一起使用。當然如果是管理celery服務,可以結合celery自身的定時任務功能,具體可移步:https://thief.one/2017/08/25/1/
參考
https://pypi.org/project/supervisor/
https://www.jianshu.com/p/9559ab642d88
http://liyangliang.me/posts/2015/06/using-supervisord