tags:

views:

42

answers:

1

hello!

i have script which must execute after every n minutes. n minutes is dynamic so that i could not set a cron job to call the script (at a specific time).

so what i did was i stored the time after every n minutes in an array so that when the script is executed, it will first check whether the current time is in the array. if it is found in the array, it continues to executes otherwise it exits.

to execute the script, i must use a cron job to run every minute to check the time in the array. unfortunately, my web host only allows 5 minutes as the least interval. so every time the script is called, i check whether the values between $current_time and $current_time + (4*60) // 4 minutes is found in the array. if it is, and if needed, i use time_sleep_until to delay the script until the time reaches the value found in the array.

so if my script executes at 10:05 and the value found in the array is 10:06, i let the script sleep until 10:06 before it continues to execute. however, if the sleep time is more than a minute or so, i get a Mysql server gone away.

how can i prevent this? or is there a better way to do this?

thanks!

+2  A: 

A couple choices, which is better I do not know.

One, is make sure your script works with CLI and after that minute is up call it with the http://www.php.net/exec function (if your host allows it).

Two, is setup a script, with a possible hash as a key and use a header redirect after the minute is up, this would call the script brand new so a new MySQL connection is made.

A third option is to set the script up like in two, except setup a schedule task / cron job on your computer that opens that page (it would have to be in the webroot) and calls it every minute or however you want. This is not a set method, but depends on how much your computer is on.

Fourth, similar to the third but use a free cron job hosting service like: http://www.onlinecronjobs.com/en

Hope that helps. If I think of other options I will update.

Brad F Jacobs
i will check the first two as i am not familiar with it, i can't go with the third though. and i didn't know there are online services such as this, which is nice. thanks for the help!
user398651
i used your 2nd suggestion for a similar problem - this time it was the maximum execution time, thanks!
user398651