您的位置:首页 > 编程语言 > PHP开发

用php中的ignore_user_abort实现计划任务

2009-04-21 17:28 786 查看
今天一不小心在php手册上发现了这个函数-ignore_user_abort,这个函数可以帮助我们实现像linux中的cron一样实现计划任务,下面一起来看下该如何来实现。

首先看下php手册对这个函数的解释

Description

int [b]ignore_user_abort[/b] ([ bool $setting ] )
Sets whether a client disconnect should cause a script to be aborted.

也就是说无论客户端是否关闭浏览器,下面的程序都会执行.
再看下其参数

Parameters

setting
If not set, the function will only return the current setting.

这个函数接受一个参数,来决定是否启用[b]ignore_user_abort[/b]的功能。
再看其返回值:

Return Values

Returns the previous setting, as a boolean.

这里说返回前一次的设置,并且是bool值得,经过我的测试,这个说法是不对的,返回的明明是int型的,不相信的话大家可以写一个php文件来测试下。
说了这么多了,到底该如何用php的这个函数实现计划任务呢?还跌借助另外一个函数,这个函数是set_time_limit,通过set_time_limit0)可以设置程序的执行时间为无限制,php默认的执行时间是30秒,通过set_time_limit(0)可以让程序无限制的执行下去。在程序执行之前加上[b]ignore_user_abort[/b](1)和set_time_limit(0)即可以了,最终程序该如何写呢?给大家一个例子。

ignore_user_abort(); // run script in background

set_time_limit(0); // run script forever

$interval=60*15; // do every 15 minutes...

do{

// add the script that has to be ran every 15 minutes here

// ...

sleep($interval); // wait 15 minutes

}while(true);

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: