We have the following monit config which restarts tomcat if unable to connect to it:
check host Tomcat-Foo with address localhost
stop program = "/usr/bin/systemctl stop tomcat.service"
start program = "/usr/bin/systemctl start tomcat.service" with timeout 360 seconds
if failed host localhost
port 8081
protocol http
request "/foo/"
for 3 times within 5 cycles
then alert
The problem is that it takes quite a while to start up, and monit seems to keep checking. This means that while tomcat is starting up, monit seems to think it is down "again" and initiate another restart, turning it into a restart loop.
Is there an easy way to have monit pause/disable the checking until tomcat is in fact back up again?
Alternatively if this config should look completely different so it wasn't an issue to begin with?
Try this:
When monit performs its checks, it will only check Tomcat-Foo every 2 cycles, giving it more time to start up. Adjust the number of cycles if you require more/less time.
This is the "slightly" hackish solution we have currently. Basically, if Tomcat doesn't start up within the cycle and is therefore restarted again (and again, and again...), the
if N restarts
check runs a script that turns off monitoring for a while.We also changed the monit configuration to target the tomcat process as well, so it's not just a host check.
Monit config
monit-wait.sh
Not particularly pretty, but seems to work at least. The alternative could of course be to use this script as the failed action, but yeah... anyways, better suggestions are welcome still :)
Just add timeout on restart, and a sleep in a start script. For some reason, "&& sleep 5m" in the start command does not work.. it would be nice figure out a way to delay the start command.
Also note that if you have Apache in front of Tomcat, the host check will always succeed!.. so http-check.sh below works by checking for a keyword.
/etc/monit/bin/tomcatstart.sh
/etc/monit/conf-enabled/tomcat8
/etc/monit/bin/http-check.sh
works as expected, waits 5 minutes without trying again.