php计划任务示例详解
面介绍使用Linux中的Crontab来实现的两种方法。关于crontab的使用,可参考文章:Linux计划任务crontab详解
一、在Crontab中使用PHP执行脚本
就像在Crontab中调用普通的shell脚本一样(具体Crontab用法),使用PHP程序来调用PHP脚本。
每一小时执行myscript.php如下:
<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('copy8605')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8605>#crontab -e
00 * * * * /usr/local/bin/php /home/web/abc.php/usr/local/bin/
php为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('copy1725')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1725>00 * * * * lynx -dump http://abc.cn/script.php
下面的例子是使用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('copy8330')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8330>*/5 * * * * /usr/bin/curl -o temp.txt http://abc.cn/script.php
下面的例子是使用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('copy5138')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5138>*/10 * * * * /usr/bin/wget -q -O temp.txt http://abc.cn/script.php
再介绍一下在Windows下php计划任务的实现
1、新建一个abc.php,内容如下:
<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('copy9692')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9692><?
$fp = fopen("abc.txt", "a ");
fwrite($fp, date("Y-m-d H:i:s") . " 成功了!/n");
fclose($fp);
?>
2、新建abc.bat文件,内容如下:
<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('copy5170')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5170>C:/php/php.exe -q D:/web/abc.php
3、建立WINDOWS计划任务:
开始–>控制面板–>任务计划–>添加任务计划
浏览文件夹选择上面的abc.bat文件
设置时间和密码(登陆WINDOWS的保存即可了。
4、右键点击计划任务 -> “运行”即可
再介绍一个使用php程序来实现的计划任务
这种效率并不高,也不稳定,使用 crontab 才是王道。
<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('copy7366')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7366><?php
ignore_user_abort(); // 关掉浏览器,PHP脚本也可以继续执行
set_time_limit(3000); // 设置程序的执行时间为3000秒
// set_time_limit(0); // 设置程序的执行时间为无限长
$interval = 30; // 每隔30秒运行
do {
$fp = fopen('abc.txt', 'a'); // 打开 abc.txt
fwrite($fp, '你好啊,我来自 http://www.jiaochengji.com'); // 往 abc.txt 写入信息
fclose($fp); // 关闭 abc.txt
sleep($interval); // 等待30秒
} while (true);
?>
首先运行该页面,然后关闭该页面,程序仍然运行中,每隔30秒程序会填补信息到abc.txt文件中去。
您可能感兴趣的文章:
深入php定时计划任务的实现方法详解
php定时计划任务的小例子
php定时计划任务实例教程
php定时执行任务的简单实例
自动删除mysql死连接 mysql死锁
php计划任务示例详解
PHP实现定时任务的几种处理方法
windows中计划任务定时执行PHP文件方法
windows定时执行PHP文件
php定时计划任务 ignore_user_abort的用法举例