How do I run a PHP script continuously on localhost?
I have a script which crawls info using API. What I want is to run this script continuously to grab data and store it on my local machine. I configured machine as localh开发者_运维技巧ost and installed phpmyadmin and MySQL.
You probably will want to run it from the command line. Put your code in some kind of loop to always keep it running, or schedule it to run as a cron job / scheduled task.
Wrap your main code, except for functions, classes, etc, in this:
while (true) {
and this:
sleep(2);
}
Then run it from the command line, using:
$ php myscript.php
If using Windows, you manually have to add php to your PATH. I don't know Windows (using a Mac all my life) but I guess you can do this in My Machine's Properties. :)
Read about set_time_limit, or modify max_execution_time
value in your php.ini
file .
Instead of a php loop, use a batch file loop. .
This gets around any set_time_limit , maximum memory etc problems. If php crashes, no problem, a new instance will be started immediately.
@echo off
:loop
php myScrapeScript.php
goto loop;
精彩评论