如何用ThinkPHP创建定时任务实例教程?

2026-04-28 23:463阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计728个文字,预计阅读时间需要3分钟。

如何用ThinkPHP创建定时任务实例教程?

本章节介绍了使用ThinkPHP实现定时任务的方法,以及与cron实现定时任务的对比。希望对学习ThinkPHP的朋友有所帮助!

ThinkPHP实现定时任务案例:

1.创建定时任务控制器

2.定义定时任务方法

3.在配置文件中设置定时任务调度

cron定时任务常见类型:

1.每分钟执行:* * * * *

2.每小时执行:0 * * * *

3.每天执行:0 0 * * *

4.每月执行:0 0 1 * *

本篇文章介绍了使用ThinkPHP实现定时任务的方法,和cron实现定时任务的方法,希望对学习thinkphp的朋友有帮助!

ThinkPHP实现定时任务案例

定时任务常见的是Linux中的crontab定时任务,这种是通过编写脚本来执行的,它会在后台一直循环执行。但是有时候我们没有服务器权限或者说我们没有独立的服务器,那又该怎么办?其实,定时任务还有一种就是被动是,只要访问项目就会触发,被动式定时任务一般用于虚拟主机,因为没有服务器权限我们只能通过代码来实现。下面我们以thinkPHP为例来分析这两种定时任务的区别。

(推荐教程:thinkphp教程)

被动式定时任务

①、tags.php

在/Application/Common/Conf目录下新建tags.php文件。(此和方法一处一样)

<?php return array( //'配置项'=>'配置值' 'app_begin' =>array('Behavior\CronRunBehavior'), );

②、crons.php

在/Application/Common/Conf目录下新建crons.php文件。(此处和方法一有区别,注意区分。)

<?php return array( //myplan为我们计划定时执行的方法文件,2是间隔时间,nextruntime下次执行时间 //此文件位于/Application/Cron/目录下 'cron' => array('myplan', 2, nextruntime), );

③、myplan.php

在/Application/Common/目录下新建 Cron文件夹,里面新建文件myplan.php文件。

<?php echo date("Y-m-d H:i:s")."执行定时任务!" . "\r\n<br>";

此时我们就可以访问项目的url,然后我们会发现在Application/Runtime/目录下生成了~crons.php文件,同时页面出现如下效果,文件内容如下:

<?php return array ( 'cron' => array ( 0 => 'myplan', 1 => 2, 2 => 1502089802, ), ); ?>

主动式定时任务

①、登录Linux服务器

如何用ThinkPHP创建定时任务实例教程?

[root@iZwz924w5t4862mn4tgcyqZ ~]# crontab -e */1 * * * * /usr/local/php/bin/php /data/wwwroot/door/test.php//执行PHP文件 */1 * * * * /usr/bin/curl www.100txy.com/wechatapi.php//访问url

②、编辑test.php

<?php $txt = "/data/wwwroot/door/test.txt"; // die(var_dump($txt)); $date=date('Y-m-d H:i:s',time()); $content = file_get_contents($txt); if($content!=''){ $arr=explode('#',$content); $num=$arr['1']+1; $string=$date.'#'.$num; }else{ $string=$date.'#'.'1'; } file_put_contents($txt,$string); $content_last = file_get_contents($txt); return $content_last;

③、后台监测test.txt文件

[root@iZwz924w5t4862mn4tgcyqZ ~]# tail -f /data/wwwroot/door/test.txt

(免费学习视频教程分享:php视频教程)

以上就是ThinkPHP实现定时任务案例的详细内容,更多请关注自由互联其它相关文章!

本文共计728个文字,预计阅读时间需要3分钟。

如何用ThinkPHP创建定时任务实例教程?

本章节介绍了使用ThinkPHP实现定时任务的方法,以及与cron实现定时任务的对比。希望对学习ThinkPHP的朋友有所帮助!

ThinkPHP实现定时任务案例:

1.创建定时任务控制器

2.定义定时任务方法

3.在配置文件中设置定时任务调度

cron定时任务常见类型:

1.每分钟执行:* * * * *

2.每小时执行:0 * * * *

3.每天执行:0 0 * * *

4.每月执行:0 0 1 * *

本篇文章介绍了使用ThinkPHP实现定时任务的方法,和cron实现定时任务的方法,希望对学习thinkphp的朋友有帮助!

ThinkPHP实现定时任务案例

定时任务常见的是Linux中的crontab定时任务,这种是通过编写脚本来执行的,它会在后台一直循环执行。但是有时候我们没有服务器权限或者说我们没有独立的服务器,那又该怎么办?其实,定时任务还有一种就是被动是,只要访问项目就会触发,被动式定时任务一般用于虚拟主机,因为没有服务器权限我们只能通过代码来实现。下面我们以thinkPHP为例来分析这两种定时任务的区别。

(推荐教程:thinkphp教程)

被动式定时任务

①、tags.php

在/Application/Common/Conf目录下新建tags.php文件。(此和方法一处一样)

<?php return array( //'配置项'=>'配置值' 'app_begin' =>array('Behavior\CronRunBehavior'), );

②、crons.php

在/Application/Common/Conf目录下新建crons.php文件。(此处和方法一有区别,注意区分。)

<?php return array( //myplan为我们计划定时执行的方法文件,2是间隔时间,nextruntime下次执行时间 //此文件位于/Application/Cron/目录下 'cron' => array('myplan', 2, nextruntime), );

③、myplan.php

在/Application/Common/目录下新建 Cron文件夹,里面新建文件myplan.php文件。

<?php echo date("Y-m-d H:i:s")."执行定时任务!" . "\r\n<br>";

此时我们就可以访问项目的url,然后我们会发现在Application/Runtime/目录下生成了~crons.php文件,同时页面出现如下效果,文件内容如下:

<?php return array ( 'cron' => array ( 0 => 'myplan', 1 => 2, 2 => 1502089802, ), ); ?>

主动式定时任务

①、登录Linux服务器

如何用ThinkPHP创建定时任务实例教程?

[root@iZwz924w5t4862mn4tgcyqZ ~]# crontab -e */1 * * * * /usr/local/php/bin/php /data/wwwroot/door/test.php//执行PHP文件 */1 * * * * /usr/bin/curl www.100txy.com/wechatapi.php//访问url

②、编辑test.php

<?php $txt = "/data/wwwroot/door/test.txt"; // die(var_dump($txt)); $date=date('Y-m-d H:i:s',time()); $content = file_get_contents($txt); if($content!=''){ $arr=explode('#',$content); $num=$arr['1']+1; $string=$date.'#'.$num; }else{ $string=$date.'#'.'1'; } file_put_contents($txt,$string); $content_last = file_get_contents($txt); return $content_last;

③、后台监测test.txt文件

[root@iZwz924w5t4862mn4tgcyqZ ~]# tail -f /data/wwwroot/door/test.txt

(免费学习视频教程分享:php视频教程)

以上就是ThinkPHP实现定时任务案例的详细内容,更多请关注自由互联其它相关文章!