2012-09-24

24 min interval for Cron Job and cycling through file

 Simple approach, how to make cron job iterating over a list of a variables. Firstly, it is necessary store the list somewhere permanently. In simpliest case it can be text document and here the trick is to take only information from first line and then to put it on the end of file as a last element. array_push copies the first line of file $farray[0] as a last element of $farray, and then array_shift removes this first element.
$farray=file("file.txt");
$thisIsWhatINeed=$farray[0];
array_push($farray,$farray[0]);
array_shift($farray);
file_put_contents('file.txt', implode("",$farray));
I added a condition, because in my case there could be, that some part of cron job should not be executed at certain times. I have 54 runs per 24 hours; I calculated that cron job must run every 24 minutes. In 24 hours there are 1440 minutes; 54 runs could take maximum interval 26.666 minutes, but for convenience creating cron jobs (which is another aspect, written a little bit later) I set interval for 24 minutes between cron jobs and add 6 empty runs, which were taken in account using condition. Finally I had 60 runs for 24 hours, so i have 24 minutes as a equal interval between cron job runs.
And here comes another aspect - it is impossible to set intervals in cron, only set times. Thus i had to create 5 cron jobs with each defining (with 24 min step) when to start and run after 2 hours (because in 2 hours there can be 5x24 min intervals)

Nav komentāru:

Ierakstīt komentāru