![当文件的校验和发生改变时,使用 monit 重新启动进程](https://linux22.com/image/700878/%E5%BD%93%E6%96%87%E4%BB%B6%E7%9A%84%E6%A0%A1%E9%AA%8C%E5%92%8C%E5%8F%91%E7%94%9F%E6%94%B9%E5%8F%98%E6%97%B6%EF%BC%8C%E4%BD%BF%E7%94%A8%20monit%20%E9%87%8D%E6%96%B0%E5%90%AF%E5%8A%A8%E8%BF%9B%E7%A8%8B.png)
当文件校验失败时,我想重新启动 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
块中的操作不会执行任何操作。
答案1
简单来说就是行动重新开始
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'