Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to start a program (Resque) but it takes a bit of time before a pidfile is written. Thus, I think that Monit thinks the program hasn't started and starts one or two more programs before the before the pidfile of the first one is written.

How do I delay the time Monit checks again, just for this process? Or should I solve this in another way?

share|improve this question
I added a new answer below. Although waiting longer between checks will prevent collisions for slow services, it can be a really bad experience for customers. – Eddie Mar 21 '15 at 21:14
up vote 6 down vote accepted

How do I delay the time Monit checks again, just for this process?

What you are trying to achieve could be done via "SERVICE POLL TIME" feature of monit

Monit documentation says

Services are checked in regular intervals given by the

set daemon n

statement. Checks are performed in the same order as they are written in the .monitrc file, except if dependencies are setup between services, in which case the services hierarchy may alternate the order of the checks.

One of the method to customize service poll is

  1. custom interval based on poll cycle length multiple



check process resque with pidfile /your/app/root/tmp/pid/
   every 2 cycles

Or should I solve this in another way?

I also did initial attempt to monitor resque jobs with monit because monit is a very lightweight daemon but eventually settled with GOD. I know , I know GOD is more resource hungry in comparison to monit but in case of resque we found it to be a good match.

share|improve this answer
Thanks! I ended up using every x cycles. I just found the number that worked for me. – Ramon Tayag May 27 '12 at 9:03

You can check a specific service on a different interval than the default...

See SERVICE POLL TIME in the Monit documentation.

An example for your Resque program would be to check on a different number of cycles:

check process resque with pidfile /var/run/
   every 5 cycles

or from the examples section:

Some servers are slow starters, like for example Java based Application Servers. 
So if we want to keep the poll-cycle low (i.e. < 60 seconds) but allow some services to take its time to start, 
the every statement is handy:

 check process dynamo with pidfile /etc/ every 2 cycles
       start program = "/etc/init.d/dynamo start"
       stop program  = "/etc/init.d/dynamo stop"
       if failed port 8840 then alert

or you can leverage the cron-style checks.

check process resque with pidfile /var/run/
   every 10 * * * *

or if you're experiencing a slow startup, you can extend the timeout in the service start command:

check process apache with pidfile /var/run/
       start program = "/etc/init.d/httpd start" with timeout 90 seconds
share|improve this answer
Thanks ewwhite, this is useful too. – Ramon Tayag May 27 '12 at 13:41
Same answer, right? – ewwhite May 27 '12 at 14:11
with timeout 90 seconds was exactly what I wanted. Thanks. – andrew Apr 29 '15 at 0:41

A member of my team came up with a rather clever solution that allows monit to check frequently (every minute), but once it has attempted to restart the service (which takes ~10 minutes) it will wait a specified grace period before attempting to start again.

This prevents waiting too long between checks, which combined with slow start is a much larger impact to customers. It works by using an intermediate script that acts as flag to indicate monit is already taking action from the last failure.

check host bamboo with address
   if failed
           port 443 type tcpSSL protocol http
           and status = 200
           and request /about.action
            for 3 cycles
   then exec "/bin/bash -c 'ps -ef | grep -v "$$" | grep -v "grep" | grep >/dev/null 2>&1; if [ $? -ne 0 ]; then /opt/monit/scripts/; fi'"

If bamboo (slow starting web app) is down for 3 minutes in a row, restart, BUT only if a restart script is not already running.

The the script that is called has a specified sleep that waits LONGER then the slowest start time for the service (in our case we expect to finish in ~10, so we sleep for 15)

echo "Retarting bambo by calling init.d"
/etc/init.d/bamboo stop
echo "Stopped completed, calling start"
/etc/init.d/bamboo start
echo "Done restarting bamboo, but it will run in background for sometime before available so, we are sleeping for 15 minutes"
sleep 900
echo "done sleeping"
share|improve this answer

You can also check if something have failed for X times straight:

 if failed 
    port 80 
    for 10 cycles 
 then alert

Or for X times within Y polls:

 if failed 
    port 80
    for 3 times within 5 cycles 
 then alert

Or both:

 check filesystem rootfs with path /dev/hda1
  if space usage > 80% for 5 times within 15 cycles then alert
  if space usage > 90% for 5 cycles then exec '/try/to/free/the/space'

(from here)

share|improve this answer

The current version of Monit (5.16) supports a timeout for the start scripts with the syntax:

 <START | STOP | RESTART> [PROGRAM] = "program"
    [[AS] UID <number | string>]
    [[AS] GID <number | string>]
    [[WITH] TIMEOUT <number> SECOND(S)]

The docs state:

In the case of a process check, Monit will wait up to 30 seconds for the start/stop action to finish before giving up and report an error. You can override this timeout using the TIMEOUT option.

Which is what the "timeout" value will do.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.