Home  >  Article  >  Backend Development  >  PHP定时后台执行程序脚本

PHP定时后台执行程序脚本

WBOY
WBOYOriginal
2016-06-20 13:03:191795browse

PHP定时执行的小代码,关掉浏览器,PHP脚本也能后继续执行!

ignore_user_abort();//关掉浏览器,PHP脚本也可以继续执行
set_time_limit(20);//程序超时时间,单位秒;通过set_time_limit(0)可以让程序无限制的执行下去;当用了set_time_limit()函数设置运行时间,sleep()函数在执行程序时的持续时间将会被忽略掉
$interval=5;//每隔多少秒运行,单位:秒
do{
//这里是你要执行的代码,这里是在一个number.txt的文本里生成数字
$i = 1;
$num_file = "number.txt";
$fp = fopen($num_file,"r");
$buf = fread($fp,filesize($num_file));
fclose($fp);

$number = file_get_contents($num_file);
$number = $number+$i;
$fp = fopen($num_file,"w");
fwrite($fp,$number);
fclose($fp);
echo $number;

//等待执行的时间
sleep($interval);
}
while(true);

 


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn