Remember to set the maximum execution time to infinite (0).
It’s better to make sure that you do not run more than one instance if this is your intention:
ignore_user_abort(true);
set_time_limit(0);
$hLock=fopen(__FILE__.".lock", "w+");
if(!flock($hLock, LOCK_EX | LOCK_NB))
die("Already running. Exiting...");
while(true)
{
usleep(2000);
}
flock($hLock, LOCK_UN);
fclose($hLock);
unlink(__FILE__.".lock");
If in CLI mode just run the file.
If in another PHP on a web server you can run one that should work endlessly like this (instead of using cURL, this removes the dependency):
$cx=stream_context_create(
array(
"http"=>array(
"timeout" => 1,
"ignore_errors" => true
)
)
);
@file_get_contents("http://localhost/infinite_loop.php", false, $cx);
Or you can start with linux cron using wget as follows:
`* * * * * wget -O - http:`
Or you can start with Windows Scheduler using bitadmin, which launches a .bat file that contains the following:
bitsadmin /create infiniteloop
bitsadmin /addfile infiniteloop http://localhost/infinite_loop.php
bitsadmin /resume infiniteloop
source
share