我们之前为大家介绍了php计划任务的原理,以及实现定时执行计划任务,有时我们在项目中需要对远程数据库上做处理,这时我们就要使用到php计划任务,今天就给大家介绍下php计划任务的处理实例!
本次使用php实现计划任务主要使用了 ignore_user_abort() set_time_limit(0) sleep() 这三个函数。
例子
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#fb7">
<tr>
<td width="464" height="27" bgcolor="#ffe7ce"> 代码如下</td>
<td width="109" align="center" bgcolor="#ffe7ce" style="cursor:pointer;" onclick="docopy('copy1032')">复制代码</td>
</tr>
<tr>
<td height="auto" colspan="2" valign="top" bgcolor="#ffffff" style="padding:10px;" class="copyclass" id=copy1032>
<?php
ignore_user_abort(); //即使client断开(如关掉浏览器),php脚本也可以继续执行.
set_time_limit(0); // 执行时间为无限制,php默认的执行时间是30秒,通过set_time_limit(0)可以让程序无限制的执行下去
$interval=60*5; // 每隔5分钟运行
do{
$fp = fopen('test.txt','a');
fwrite($fp,'test');
fclose($fp);
sleep($interval); // 等待5分钟
}while(true);
?>
</td> </tr> </table>
具体的代码如下:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#fb7">
<tr> <td width="464" height="27" bgcolor="#ffe7ce"> 代码如下</td>
<td width="109" align="center" bgcolor="#ffe7ce" style="cursor:pointer;" onclick="docopy('copy7070')">复制代码</td>
</tr>
<tr>
<td height="auto" colspan="2" valign="top" bgcolor="#ffffff" style="padding:10px;" class="copyclass" id=copy7070>
<?php
ignore_user_abort();//该函数的作用是当用户关掉浏览器后,php脚本也可以继续执行.
set_time_limit(3000);// 通过set_time_limit(0)可以让程序无限制的执行下去
$interval=5;// 每隔5s运行
//方法1--死循环
do{
echo '测试'.time().'<br/>';
sleep($interval);// 等待5s
}while(true);
//方法2---sleep 定时执行
require_once './curlclass.php';//引入文件
$curl=new httpcurl('www.phpernote.com');//实例化
$stime=$curl->getmicrotime();
for($i=0;$i<=10;$i ){
echo '测试'.time().'<br/>';
sleep($interval);// 等待5s
}
ob_flush();
flush();
$etime=$curl->getmicrotime();
echo '<hr>';
echo round(($etime-stime),4);//程序执行时间
</td> </tr> </table>
函数int ignore_user_abort :
从函数名本身,可以解释为,"忽略用户的影响"
因为所谓的用户是指客户端,即浏览器
所以进一步解释为,"忽略浏览器的影响"
那么影响指的是什么,影响指的是浏览器的关闭和异常
也就是说有这个函数在的php程序,即使在浏览器关掉的时候,程序没有执行完它还会继续执行,直到执行完
比如说,你有一段代码需要执行100秒,可是这个时间太长了,一般用户等不及,在等了60秒的时候受不了就关了
如果这个时候程序也随之终止,很可能造成数据异常,不一致或是错误,你需要程序继续运行,就可以用它了
它的参数就是真和假,真就是忽略,假就是不忽略
在具体的实现过程中个人感觉php定时执行任务的效率并不高,建议关于定时执行任务的工作还是交给shell来做吧,相对来说,这个方法实现的太过勉强,而shell是专业级别的了。
2、linux的脚本程序
这里主要使用到crontab这个命令,
使用方式 :
crontab filecrontab [ -u user ] [ -u user ] { -l | -r | -e }
说明 :
crontab 是用来让使用者在固定时间或固定间隔执行程式之用
使用crontab写shell脚本,然后让php调用shell,这个是利用linux的特性,应该还不算php自身语言的特性
在crontab中使用url执行脚本
如果你的php脚本可以通过url触发,你可以使用lynx或curl或wget来配置你的crontab。
下面的例子是使用lynx文本浏览器访问url来每小时执行php脚本。lynx文本浏览器默认使用对话方式打开url。但是,像下面的,我们在lynx命令行中使用-dump选项来把url的输出转换来标准输出。
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#fb7">
<tr> <td width="464" height="27" bgcolor="#ffe7ce">
代码如下</td>
<td width="109" align="center" bgcolor="#ffe7ce" style="cursor:pointer;" onclick="docopy('copy7213')">复制代码</td>
</tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#ffffff" style="padding:10px;" class="copyclass" id=copy7213>
00 * * * * lynx -dump http://www.111cn.net /script.php
</td>
</tr>
</table>
下面的例子是使用curl访问url来每5分执行php脚本。curl默认在标准输出显示输出。使用”curl -o”选项,你也可以把脚本的输出转储到临时文件。
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#fb7">
<tr>
<td width="464" height="27" bgcolor="#ffe7ce">
代码如下</td>
<td width="109" align="center" bgcolor="#ffe7ce" style="cursor:pointer;" onclick="docopy('copy6465')">复制代码</td>
</tr>
<tr>
<td height="auto" colspan="2" valign="top" bgcolor="#ffffff" style="padding:10px;" class="copyclass" id=copy6465>
*/5 * * * * /usr/bin/curl -o temp.txt http://www.111cn.net /script.php</td>
</tr>
</table>
下面的例子是使用wget访问url来每10分执行php脚本。-q选项表示安静模式。”-o temp.txt”表示输出会发送到临时文件。
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#fb7">
<tr>
<td width="464" height="27" bgcolor="#ffe7ce">
代码如下</td>
<td width="109" align="center" bgcolor="#ffe7ce" style="cursor:pointer;" onclick="docopy('copy1200')">复制代码</td>
</tr>
<tr>
<td height="auto" colspan="2" valign="top" bgcolor="#ffffff" style="padding:10px;" class="copyclass" id=copy1200>
*/10 * * * * /usr/bin/wget -q -o temp.txt http://www.111cn.net /script.php
</td> </tr> </table></td> </tr> </table>
总结:
本文是通过实际的项目开发过程实现的php计划任务的实例,对你们的开发工作有一定的帮助!
相关推荐:
php如何实现定时执行计划任务?
php实现计划任务的原理分析
php实现计划任务与持续进程实例 fsockopen
php 计划任务 检测用户连接状态
以上就是php计划任务的示例代码分享的详细内容。