当前位置:首页 > PHP > 正文内容

thinkphp5定时任务,tp5定时任务,thinkphp定时任务,php定时任务,php定时器

高老师5年前 (2020-03-01)PHP7390

本教程使用的定时任务基于EasyTak,EasyTask官方文档:https://gitee.com/392223903/EasyTask

(1).安装tp5.0或者5.1

composer create-project topthink/think=5.0.* tp5  --prefer-dist

(2).安装定时任务composer包

composer require easy-task/easy-task

(3).创建命令行处理类文件application/common/command/Task.php

<?php
namespace app\common\command;

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

class Task extends Command
{
    protected function configure()
    {
        //设置名称为task
        $this->setName('task')
            //增加一个命令参数
            ->addArgument('action', Argument::OPTIONAL, "action")
            ->addArgument('force', Argument::OPTIONAL, "force");
    }

    protected function execute(Input $input, Output $output)
    {
        //获取输入参数
        $action = trim($input->getArgument('action'));
        $force = trim($input->getArgument('force'));

        // 配置任务
        $task = new \EasyTask\Task();
        $task->setRunTimePath('./runtime/');
        $task->addFunc(function () {
            $url = 'https://blog.20230611.cn/?id=319';
            file_get_contents($url);
        }, 'request', 10, 2);;

        // 根据命令执行
        if ($action == 'start')
        {
            $task->start();
        }
        elseif ($action == 'status')
        {
            $task->status();
        }
        elseif ($action == 'stop')
        {
            $force = ($force == 'force'); //是否强制停止
            $task->stop($force);
        }
        else
        {
            exit('Command is not exist');
        }
    }
}

(4).将上面创建的Task.php在配置文件application/command.php中配置一下

return [
    'app\common\command\Task',
];

(5).执行命令(windows请使用cmd):

php think task start  启动命令
php think task status 查询命令
php think task stop   关闭命令
php  think  task  stop  force   强制关闭命令

上面创建的定时任务是每隔10秒访问2次网站地址。

提示:后台执行失败可修改为前台启动查看问题或者查看日志文件,有问题可以在qq群反馈bug,记得用星星支持我们

扫描二维码推送至手机访问。

版权声明:本文由高久峰个人博客发布,如需转载请注明出处。

本文链接:https://blog.20230611.cn/post/125.html

分享给朋友:

“ thinkphp5定时任务,tp5定时任务,thinkphp定时任务,php定时任务,php定时器” 的相关文章

php mcrypt扩展被废弃的解决方案

php mcrypt扩展被废弃的解决方案

使用openssl扩展对应替换mcrypt的函数,(比较麻烦,但是openssl是未来趋势)在新版php中编译mcrypt扩展使用一个纯php代码实现的mcrypt扩展库,git地址为https://github.com/phpseclib/mcrypt_compat,每个mcrypt的方法都已经实...

在MyISAM引擎中使用事务会怎样

在MyISAM引擎中使用事务会怎样

众所周知MyISAM引擎不支持事务,但是我只是知道不支持事务,并未测试具体的表现是什么,测试代码如下:try {     //开启事务     Db::startTrans();    &...

php生成器的send方法详解,php yield send

php生成器的send方法详解,php yield send

【一】.基本用法首先看看官方对send方法的解释:Sets the return value of the yield expression and resumes the generato...

php event异常处理,php set_exception_handler无效

php event异常处理,php set_exception_handler无效

(1).今天遇到一件奇怪的事情,在event事件中是无法自定义异常处理,例如我们使用set_exception_handler来统一处理异常。例如下面的代码:<?php error_reporting(E_ALL); set_error_handler(function ($errn...

php设置进程标题,php设置进程名称

php设置进程标题,php设置进程名称

    /**      * 设置进程标题      * @param string $title  &nbs...

php levenshtein,php字符串编辑距离

php levenshtein,php字符串编辑距离

levenshtein函数可以计算两个字符串之间的编辑距离,那么何为编辑距离?编辑距离概念:编辑距离,是指两个字串之间,通过替换or插入or删除等操作将字符串str1转换成str2所需要操作的最少字符数量。 该算法的复杂度是 O(m*n),其中 n 和 m 分别是str1 和str2的长度注意:如果...