天天看點

supervisord 監控 mysql_使用supervisor監控程序

在linux下監控程序,可以使用inittab,最近找到了supervisor,也很好用,記錄一下:

1、系統要安裝python,并安裝與之對應的setuptools,下載下傳位址在此

2、安裝:

# sh setuptoolsxxxx.egg

3、安裝supervisor,下載下傳位址在此,解壓縮後

# python setup.py install

這就ok了,然後執行

# echo_supervisord_conf > /etc/supervisord.conf

修改/etc/supervisord.conf檔案,加入你要監控的程序,裡面的注釋很詳細,舉個簡單的例子:

這是一段要監控的程序的描述資訊,添加到這個檔案的末尾就好了:

[program:meta.txn.recover.on.error]

command=/cas/bin/meta.txn.recover.on.error ; 被監控的程序路徑

numprocs=1                    ; 啟動幾個程序

directory=/cas/bin                ; 執行前要不要先cd到目錄去,一般不用

autostart=true                ; 随着supervisord的啟動而啟動

autorestart=true              ; 自動重新開機。。當然要選上了

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

exitcodes=0                 ; 正常退出代碼(是說退出代碼是這個時就不再重新開機了嗎?待确定)

stopsignal=KILL               ; 用來殺死程序的信号

stopwaitsecs=10               ; 發送SIGKILL前的等待時間

redirect_stderr=true          ; 重定向stderr到stdout

為了節省空間,注釋的内容就不貼出來了。

執行

# supervisord -n

能在控制台看到監控程序的輸出:

2010-08-17 10:26:07,467 INFO supervisord started with pid 943

2010-08-17 10:26:08,469 INFO spawned: 'meta.txn.recover.on.error' with pid 1009

2010-08-17

10:26:09,876 INFO success: meta.txn.recover.on.error entered RUNNING

state, process has stayed up for > than 1 seconds (startsecs)

2010-08-17 10:26:48,442 INFO exited: meta.txn.recover.on.error (terminated by SIGKILL; not expected)

2010-08-17 10:26:49,444 INFO spawned: 'meta.txn.recover.on.error' with pid 2427

2010-08-17

10:26:50,487 INFO success: meta.txn.recover.on.error entered RUNNING

state, process has stayed up for > than 1 seconds (startsecs)

黑體的地方是我用kill -9殺掉程序後出來的,看到supervisor檢測到程序退出後又再次啟動了程序。

不帶參數運作supervisord是以daemon方式運作。

把supervisord加入到開機啟動項裡就可以完成監控程序的功能了。

【注意】:當supervisord以非daemon方式運作時,殺掉supervisord後,被監控的程序也退出了。

而以daemon方式運作,殺掉supervisord對被監控程序無影響。