thinkphp5 定时任务

17 篇文章 1 订阅

前段时间在研究thinkphp5.0版本做自动任务的时候,碰到了棘手的问题–如何做自动化任务,因为程序开始就需要一直执行,查了很多资料,都说靠php原生的死循环来做不靠谱,时间误差也没法保证,所以后面采用thinkphp5的command工具和服务器的定时任务来做:

1.新建command文件

在application/模块/新建一个command文件夹/Test.class.php

<?php
namespace app\admin\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;

class Test extends Command
{
    protected function configure(){
        $this->setName('Test')->setDescription("计划任务 Test");
    }

    protected function execute(Input $input, Output $output){
        $output->writeln('Date Crontab job start...');
        /*** 这里写计划任务列表集 START ***/

        $this->test();

        /*** 这里写计划任务列表集 END ***/
        $output->writeln('Date Crontab job end...');
    }

    private function test(){
        echo "test\r\n";
    }
}

2.配置command.php文件,位置在application/command.php

<?php
return ['app\admin\command\Test'];

3.运行test命令

打开命令行,运行php think Test命令test命令execute方法中运行的方法就会运行

4.在应用根目录新建bat文件

task.bat文件

D:
cd D:\xampp\htdocs\autobet
php think Test

5.将bat文件添加到服务器计划任务

这个根据window和Linux系统不一样,定时任务设置方法也不同,可以自行百度,我用的是本地的windows服务,详情看百度经验:Windows计划任务设置,定时执行指定脚本

ps:这里设置间隔时常,可以是1分钟到31天,可选项最低是5分钟,可以手写!

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

优小U

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值