因此,我一直在尝试让 Monit 在服务启动时运行脚本。虽然 Monit 可以毫无问题地重新启动服务,但我当然希望在我的服务需要启动时收到警报,而不仅仅是在服务无限期关闭时。
这是我的 /etc/monit/conf.d/nginx.pid 文件:
check process nginx with pidfile /var/run/nginx.pid
start program = "/usr/sbin/service nginx start"
stop program = "/usr/sbin/service nginx stop"
if failed host 127.0.0.1 port 8080 then restart
if changed pid then exec "/etc/monit/slack.rb"
if failed host 127.0.0.1 port 8080 then exec "/etc/monit/slack.rb" else if succeeded then exec "/etc/monit/slack.rb"
if cpu is greater than 40% for 2 cycles then exec "/etc/monit/slack.rb" else if succeeded then exec "/etc/monit/slack.rb"
if cpu > 60% for 5 cycles then restart
现在,如果我将端口更改为 nginx 未运行的其他端口(因此显然无法连接),monit 将运行我的 slack.rb 脚本,但是当 nginx 停止但由 monit 启动时,它不会运行该脚本。后一种情况是我需要工作的。
谢谢你的帮助!
答案1
我让它以这种方式工作:
check process nginx with pidfile /var/run/nginx.pid
start program = "/usr/sbin/service nginx start"
stop program = "/usr/sbin/service nginx stop"
IF does not exist THEN restart ELSE IF SUCCEEDED THEN exec "/etc/monit/slack.rb"
IF failed host 127.0.0.1 port 8080 THEN restart ELSE IF SUCCEEDED THEN exec "/etc/monit/slack.rb"