Home >Backend Development >PHP Tutorial >Implementation of delayed execution of PHP scheduled tasks, php delay_PHP tutorial
set_time_limit(0); //Set the maximum execution time of the script, 0 has no limit
do{
$fp = fopen('auto.txt', 'w');
if($fp)
{
$text = 'Hello nr';
$count = 0;
for($i=1; $i<10; $i++)
{
if(! $c = fwrite($fp, 'Line '.$i.': '.$text)) //Returns the number of characters written, and returns false
on failure
{
echo 'The '.$i.'th write failed!';
}
$count += $i;
}
}
fclose($fp);
sleep(60); //Delay execution for 60 seconds
}while(true);
If you want to use PHP files, you can barely do it
There is a sleep function in php. The specific use is sleep(time) where time is in seconds
First, create a php script
php
while(1){
//yourcode
sleep(3600*24);
}
?>
The yourcode here is the function you want the PHP script to execute. Although this goal can be achieved, there are sacrifices. You have to access this script through the URL at 8 o'clock in the morning, which means executing the PHP file, and this link cannot be interrupted and must continue! ! Otherwise, it will be invalid
PHP is executed only upon request, and is executed only when there is a request.
PHP needs to be triggered to execute. There is no way to keep it executing for a long time. Even if you use sleep, it still needs you to click it once before it will always exist.
The method is to use scheduled tasks , that’s right, it’s a scheduled task. Both windows/*nix have scheduled tasks. You can put php into the scheduled task and use the command line to execute the php program
In fact, there is no possibility of accessing it at 0 In this case, let PHP execute it, because PHP itself is a server-side script interpretation tool. Of course, the server can only work when someone accesses it. The author is doing the opposite. If you want to use PHP to execute desktop programs, there is no way. PHP official There is such a tool, but it is too different from the way PHP operates, so the control methods are also different. Strictly speaking, it is just PHP syntax
Additional additions to the questioner:
ignore_user_abort (1);
set_time_limit(0); The author should study these two