Running a PHP script every 5 minutes and avoiding race conditions

I have a php script that needs to be run every 5 minutes. I am currently using the cron job to run it (and it works great), but my host allows a minimum time of 15 minutes.

So my question is: can I use visitors to start running php script every 5 minutes. I can easily just record the last time it started and re-run it based on elapsed time.

However, I am concerned about the conditions of the race. It is important that the script is run only once every 5 minutes.

My script takes about 60 seconds. During this time, he writes a couple of files. If the script is run more than once, it will damage the files. Also, if I do not receive vistors for 10 minutes, then starting up once the next vistor arrives is fine.

Is there any standard way to accomplish this task?

Thanks!

+6
php cron
source share
5 answers

Do you think your script starts an endless loop with sleep to wait 5 minutes between iterations?

 for (;;) { perform_actions(); sleep(300); } 

Alternatively, you can have a file (e.g. is_running) and get an exclusive lock at the beginning of your script, which is released at the end. At least in this way you will not do anything destructive.

You can also combine these two solutions.

 $fp = fopen("is_running", "r+"); /* is it already running? */ if (! flock($fp, LOCK_EX | LOCK_NB)) return; for (;;) { perform_actions(); sleep(300); } 

And then the cron job is still running every 15 minutes. If the process is still running, it will simply fail, otherwise it will resume updating and resume updating every 5 minutes.

+6
source share

Lame is the answer to a lame situation (ISP, not a poster). Schedule 12 cron jobs, all calling the same script, each of which runs once per hour, but is called with a different mark of 5 minutes.

 00 * * * * root echo "run at :00 of every hour" 05 * * * * root echo "run at :05 of every hour" 10 * * * * root echo "run at :10 of every hour" 

etc. to: 55. But I agree with my original comment - find a new provider :)

+4
source share

If you cannot do what @Brandon suggested, I would recommend approaching it the same way I did when I wrote the daemon in PHP (not the best solution, but I was practically forced to do this).

In my case, the script also accessed the file (log) and processed it, after which it entered the results into the database. Therefore, to ensure that I do not have two files working at the same time, I created a state file on which the script received a lock and if I could not do it, if it did not succeed gracefully.

 $fh = fopen('status_file', 'w'); /** * LOCK_NB is required because otherwise your script would stall until * a lock is aquired, queing a bunch of scripts. */ if(!flock($fh, LOCK_EX | LOCK_NB)) { exit 1; // our job is done here } 
+1
source share

You can use The Fat Controller to run the script at 5 minute intervals. Unlike CRON, it guarantees the interval at which one instance of the script ends when another is launched.

0
source share

The answer to the question whether visitors can run this script is yes. You can run the script when visitors enter the page. You want to save the start time, but also the attribute to start it. This should avoid any possibility of conflict when trying to update your data. I also added an additional mail alert box that you can use if the runtime passes as you expect for a time exceeding the maximum time. Then you can send the script a warning message that your script is running for the maximum time. I personally saved these statuses in the databases, but they can also be stored in files.

0
source share

All Articles