Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Run a cron process more than every minute' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Run a cron process more than every minute
Authored by: viadd on May 16, '07 08:35:08PM
Any solution that just sleeps 15 and then does something, repeatedly, will drift away from once every 15 seconds, due to the non-zero time it takes to do something. And that can vary depending on the load on your computer.

To stay in cadence, you can just sleep however long it takes until the time is evenly divisible by 15 seconds. This takes the limited but non-zero arithmetic available to the bash shell.


#! /usr/bin/env bash
dt=15
for (( i = 0 ; i < 10000 ; i++))
  do
   now=`date +%s`
   left=`expr ${dt} - \( ${now}  % ${dt} \)`
   sleep $left
   open /path/to/getpic.app 
  done


[ Reply to This | # ]
Run a cron process more than every minute
Authored by: frankiec on May 16, '07 10:27:24PM

You rock.



[ Reply to This | # ]
Run a cron process more than every minute
Authored by: bdm on May 17, '07 03:42:57AM

Something like this is the way to go if only shell tools are to be used, but this solution has problems. If the open is fast enough, the time in seconds might be the same multiple of dt that it was before. Then sleep 0 will be executed and the open will be executed too soon. A quick fix would be to test for 0 sleep times and change them to dt. Better would be to monitor the execution starts explicitly. Get the time at the beginning, say "start" and at iteration i sleep until time start+dt*i where the first iteration is i=1.

Brendan.



[ Reply to This | # ]
Run a cron process more than every minute
Authored by: viadd on May 17, '07 09:00:55PM

The expression "${dt} - \( ${now} % ${dt} \)" always gives a value from 1 to 15 (for dt = 15). The '%' operator takes the remainder when now is divided by 15, which is always in the range 0-14 , and 15 minus that puts it in the 15-1 range, respectively.

Thus there won't be any cases of sleep 0, even if open takes 0 seconds.



[ Reply to This | # ]