Monit

當文件的校驗和發生更改時,使用 monit 重新啟動程序

  • March 27, 2017

當文件的校驗和失敗時,我想重新啟動由monit監控的程序。目前我使用

check process prosody with pidfile /var/run/prosody/prosody.pid
   depends certificate_file
   start program "/etc/init.d/prosody start"
   stop program "/etc/init.d/prosody stop"
   restart program "/etc/init.d/prosody restart"

check file certificate_file with path /etc/prosody/certs/fullchain.pem
   if changed checksum then exec "/usr/bin/monit restart prosody"

但我想有一些命令,if changed checksum then restart prosody而不是使用 monit 二進製文件exec

restart操作似乎僅限於重新啟動目前監視的程序,因此check file塊中的操作不會執行任何操作。

簡單地把動作重啟

check process prosody with pidfile /var/run/prosody/prosody.pid
   depends certificate_file
   start program "/etc/init.d/prosody start"
   stop program "/etc/init.d/prosody stop"
   restart program "/etc/init.d/prosody restart"

check file certificate_file with path /etc/prosody/certs/fullchain.pem
   if changed checksum then restart

事件重啟將傳播到所有依賴項,日誌類似於

[CEST Mar 27 11:57:30] error    : 'certificate_file' checksum was changed for /etc/prosody/certs/fullchain.pem
[CEST Mar 27 11:57:30] info     : 'certificate_file' trying to restart
[CEST Mar 27 11:57:30] info     : 'prosody ' stop: /etc/init.d/prosody stop
[CEST Mar 27 11:57:30] info     : 'prosody ' start: /etc/init.d/prosody start

請注意,Monit 將繼續使用此配置一次又一次地觸發事件 -> 因此觸發不斷重啟。

此外,似乎必須指定啟動停止操作,否則什麼都不做(可能是一個錯誤)。(用 monit 5.14 測試)


編輯:使用升級的 Monit 版本 5.21.0,行為會更好,並且沒有無休止的重啟循環

[CEST Mar 27 13:47:22] info     : 'certificate_file' trying to restart
[CEST Mar 27 13:47:22] info     : 'prosody' stop: '/etc/init.d/prosody stop'
[CEST Mar 27 13:47:22] info     : 'certificate_file' checksum has not changed
[CEST Mar 27 13:47:22] info     : 'prosody' start: '/etc/init.d/prosody start'

引用自:https://serverfault.com/questions/839010