justuptime.com - monitor your servers & websites

cron less than 1 min

You are viewing this site as a guest. Join our community to get your questions answered and share knowledge. Active members may advertise and ask for a website critique.

They have: 7 posts

Joined: Mar 2004

how do I set-up a cron for less than 1 minute?

Greg K's picture
Moderator

He has: 1,612 posts

Joined: Nov 2003

In PHP, try:

<?php
function runProgram()
{
 
// THE MAIN CODE OF PROGRAM TO GO HERE
}

// SET THE FOLLOWING TO HOW MANY TIMES TO RUN PER MINUTE

$runPerMin = 1;

$waitTime = floor(60/$runPerMin);

for ($t=0; $t<$runPerMin; $t++)
{
 
runProgram();
 
sleep($waitTime);
}

?>

Keep in mind, this script will work based upon the assumption that your main programs execution takes a very minute time to execute. Now, if you are finding that the main part of the program takes 2 seconds to execute, change the one line to read $waitTime = floor(60/$runPerMin) - 2;. When you first try this, you may want to add code that checks the time it takes to execute. On my server, cron job results get e-mailed to me, so you could change the program to be:

<?php
// THIS FUNTION ONLY NEEDED FOR TIME TESTING
function microtime_float() {
  list(
$usec, $sec) = explode(" \", microtime());
  return ((float)$usec + (float)$sec);
}

function runProgram()
{
  // THE MAIN CODE OF PROGRAM TO GO HERE
}
// SET THE FOLLOWING TO HOW MANY TIMES TO RUN PER MINUTE
$runPerMin = 1;
$waitTime = floor(60/$runPerMin);
for ($t=0; $t<$runPerMin; $t++)
{
$startTime = microtime_float();
  runProgram();
  $difTime = microtime_float() - $startTime;
echo \"Funtion Executed in \" . number_format($difTime,4) . \" seconds<br>\n\";
  sleep($waitTime);
}
?>

This will give you the execution time each time the funtion is called. Then once you have an maximum (test it to do the max it could possibly do during a run), then take it out and adjust the subtraction.

Also check with your hosting company to see if there are limits on how long a script can run, their server may somehow cut it off or not run it at all if they limit it less than 60 seconds.

-Greg

[This space intentionally left blank]

Cool Geek Supplies: www.ThinkGeek.com