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

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

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

本教程使用的定时任务基于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 base64保存为图片偷懒版本

php base64保存为图片偷懒版本

<?php $base64_body = substr(strstr($_POST[base64],','),1); $data= base64_decode($base64_body); file_put_contents($_SERVER[&q...

php经纬度计算距离

php经纬度计算距离

/**  * 计算两点地理坐标之间的距离  * @param  Decimal $longitude1 起点经度  * @param  Decimal $lati...

php将html转为pdf,php将html页面导出pdf

php将html转为pdf,php将html页面导出pdf

首先下载wkhtmltox-0.12.4_linux-generic-amd64.tar.xz   (不要下载RPM包,依赖太多,需要x-server支持),并解压,执行测试运行正常tar wkhtmltox-0.12.4_linux-generic-amd64.tar.xzcd...

PHP获取原始数据

PHP获取原始数据

在项目中需要对图片进行裁剪,前端裁剪完成发送base64给后端,但是很意外的PHP获取到的数据和前端有点差距,之前我都是先加密,后端解密,但是这次依然不行。于是使用filter_input方法轻松解决。$base64 =  filter_input(INPUT_POST...

 php header属性,php header 详解,php header的作用

php header属性,php header 详解,php header的作用

header() 函数向客户端发送原始的 HTTP 报头。(官方解释)通俗的讲header函数将参数中的字符串作为服务端的响应头来返回给客户端。什么是服务端的响应头?打开谷歌浏览器看看network中的请求response header信息即可。更多的参数百度response header即可浏览器...

抛弃salt,使用password_hash()加密

抛弃salt,使用password_hash()加密

md5/sha1+salt方式是目前各大cms常用的加密方式,虽然salt安全,但是各大md5网站也在研究这个方向,那么我们应该选择password_hash动态hash来助力,一种密码有多种hash结果.看代码模拟登陆.<?php //01.注册 $user ='zhang...