1. 程式人生 > 其它 >c++ createtoolhelp32snapshot取程序路徑_Supervisord管理程序實踐

c++ createtoolhelp32snapshot取程序路徑_Supervisord管理程序實踐

技術標籤:c++ createtoolhelp32snapshot取程序路徑javamac系統通過pid獲取程序名稱kill 程序kill程序supervisord [supervisorctl]

今天湊空研究了下Supervisord,這是一款linux程序管理工具,使用python開發,主要用於在後臺維護程序(類似master守護程序),可以實現監控程序的狀態、自動重啟程序等操作,便於一些服務的維護與監控。

安裝Supervisord

由於是用python開發的,因此使用pip安裝最為方便。

1

$ pip install supervisor

說明:安裝完成之後多了3個工具:echo_supervisord_conf、supervisorctl和supervisord。

Supervisord配置檔案

首先可以使用echo_supervisord_conf命令獲取supervisor配置模板:

1

echo_supervisord_conf > supervisord.conf

說明:該命令在當前目錄下建立了一個檔名為supervisord.conf的配置檔案,編輯配置檔案:

1

vim supervisord.conf

來看看預設配置檔案中的主要配置項:(還有一些配置不常用,可以忽略)

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

[unix_http_server]

file=/tmp/supervisor.sock ; UNIX socket 檔案,supervisorctl 會使用

;chmod=0700 ; socket 檔案的 mode,預設是 0700

;chown=nobody:nogroup ; socket 檔案的 owner,格式:uid:gid

;[inet_http_server] ; HTTP 伺服器,提供 web 管理介面

;port=127.0.0.1:9001 ; Web 管理後臺執行的 IP 和埠,如果開放到公網,需要注意安全性

;username=user ; 登入管理後臺的使用者名稱

;password=123 ; 登入管理後臺的密碼

[supervisord]

logfile=/tmp/supervisord.log ; 日誌檔案,預設是 $CWD/supervisord.log

logfile_maxbytes=50MB ; 日誌檔案大小,超出會 rotate,預設 50MB

logfile_backups=10 ; 日誌檔案保留備份數量預設 10

loglevel=info ; 日誌級別,預設 info,其它: debug,warn,trace

pidfile=/tmp/supervisord.pid ; pid 檔案

nodaemon=false ; 是否在前臺啟動,預設是 false,即以 daemon 的方式啟動

minfds=1024 ; 可以開啟的檔案描述符的最小值,預設 1024

minprocs=200 ; 可以開啟的程序數的最小值,預設 200

; 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 ; 通過 UNIX socket 連線 supervisord,路徑與 unix_http_server 部分的 file 一致

;serverurl=http://127.0.0.1:9001 ; 通過 HTTP 的方式連線 supervisord

; 包含其他的配置檔案

[include]

files = relative/directory/*.ini ; 可以是 *.conf 或 *.ini

執行以下命令啟動supervisord程序,可測試supervisord是否安裝成功並執行。

1

supervisord -c supervisord.conf

檢視系統程序中是否多了一個supervisord:

1

ps -aux | grep supervisord

配置Program

  program就是用來配置監控不同的應用程式程序的,推薦每個應用程式單獨寫一個program配置檔案,然後在supervisord.conf中通過include載入所有應用程式的配置。
這裡拿建立一個celery程序為例,首先在supervisord.conf最後一行寫入:

1

2

3

;載入/etc/supervisor/目錄下所有的配置檔案

[include]

files = /etc/supervisor/*.conf

然後建立/etc/supervisor目錄,併到目錄下建立/etc/supervisor/celery_touchscan.conf檔案,寫入:

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

;program名稱,隨便寫,但不要重複,是program的唯一標識

[program:celery_touchscan]

;指定執行目錄

directory=/root/TouchScanV2/

;執行目錄下執行命令

command=celery -A scan worker --queue=touchscan --pidfile="./log/pid.txt" --logfile="./log/scan.log" -c 10

;程序名稱

process_name=%(program_name)s_%(process_num)02d

;啟動設定

numprocs=1 ;程序數,注意:(celery程序數量,不是work數量,相當於執行了10個command命令,而不是在celery中指定-c 為10)

autostart=true ;當supervisor啟動時,程式將會自動啟動

autorestart=true ;自動重啟(當work被kill了之後會重新啟動)

;執行程式的使用者

;user=root

;startsecs=1 ;程式重啟時候停留在runing狀態的秒數

;startretries=10 ;啟動失敗時的最多重試次數

;停止訊號,預設TERM

;中斷:INT (類似於Ctrl+C)(kill -INT pid),退出後會將寫檔案或日誌(推薦)

;終止:TERM (kill -TERM pid)

;掛起:HUP (kill -HUP pid),注意與Ctrl+Z/kill -stop pid不同

;從容停止:QUIT (kill -QUIT pid)

stopsignal=INT

重啟supervisord程序:

1

supervisorctl -c supervisord.conf reload

  此時檢視系統上的程序,發現建立了一個supervisord守護程序,10個celery的work程序(celery的work程序數量取決於command命令中的-c引數以及配置檔案中的numprocs引數,numprocs引數是指執行幾次command命令,而在celery命令列中指定了需要執行的work數量)

729850ea0e063ac241923944ed965737.png0e117472515e2dd3693d1d381c11aa13.png

說明:此時如果手動kill掉celery的work程序,會發現celery的work程序會被supervisord自動重啟,只有當supervisord守護程序被kill以後,才能真正kill掉celery的work程序。

supervisord命令列操作

啟動supervisord程序

1

supervisord -c supervisord.conf

關閉supervisord程序

1

supervisorctl -c supervisord.conf shutdow #注意這裡將supervisord程序關閉,但通過supervisord啟動的程序沒有關閉

重啟supervisord程序

1

supervisorctl -c supervisord.conf reload

檢視程序狀態

1

supervisorctl

效果如下:

4a7bea05e20eab17927c30bcc62f3e52.png

每列分別代表:programe名稱、程序名稱,程序狀態、程序id,執行時間

更多supervisorctl命令

1

2

3

4

5

6

$ supervisorctl status

$ supervisorctl stop celery_touchscan # celery_touchscan是一個program的名稱

$ supervisorctl start celery_touchscan

$ supervisorctl restart celery_touchscan

$ supervisorctl reread

$ supervisorctl update

說明:可以直接在系統shell中執行,也可以先執行supervisorctl,進入supervisorctl_shell中執行相應的命令。

針對Python環境

如果專案使用了python的pyenv模組來設定環境,則supervisord配置檔案中需要指定python環境的路徑。其中有兩種方式指定程式使用的Python環境:

  • command使用絕對路徑。

  • 通過environment配置PYTHONPATH。

使用supervisord注意點

子程序問題

有時候用Supervisor託管的程式還會有子程序,如果只殺死主程序,子程序就可能變成孤兒程序。通過以下這兩項配置來確保所有子程序都能正確停止:

1

2

stopasgroup=true

killasgroup=true

配置更新

每次修改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