[php] 定时执行任务

时间:2020-11-28 07:51:44


定时执行任务


一、每隔5s输入一次字符串

set_time_limit(0);  // 设置为0,表示没有时间限制,默认的最大时间限制在php.ini里有设置max_execution_time = 30
$str = 'hello world !';
while(isset($str)) {
echo $str;
flush();
ob_flush();
sleep(5);
}


二、定时把数据保存到文件内,无视客户端(浏览器)是否关闭

set_time_limit(0);
ignore_user_abort(true); // 即使浏览器关闭,也会继续执行
$interval = 5;
$stop = 1;
do {
if( $stop == 10 ) break;
file_put_contents('./Public/file/test.txt',' Current Time: '.time().' Stop: '.$stop, FILE_APPEND);
$stop++;
sleep ($interval);
} while ( true );


test.txt文件内容:


[php] 定时执行任务


3、可以使用 windows系统的[任务计划程序],实现系统定时执行程序,操作执行浏览选择已写好的批处理命令执行脚本文件


谢谢关注~