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

laravel定时任务,tp定时任务,thinkphp定时任务

高老师7年前 (2020-03-01)PHP3501

本文基于Laravel Framework 6.17.1版本

(1).创建项目(依赖真多,如此臃肿)

composer create-project --prefer-dist laravel/laravel blog

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

 composer require think-task/think-task

(3).执行创建命令行处理类文件(自动生成了文件app/Console/Commands/Task.php)

php artisan make:command Task

(4).修改第3步创建的app/Console/Commands/Task.php为以下内容

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class Task extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'task{action=start}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $action = $this->argument('action');
        // 初始化EasyTask
        $task = new \ThinkTask\Task();

        // 设置项目名称
        $task->setPrefix('Think');

        // 设置后台运行
        $task->setDaemon(false);

        // 设置日志保存目录
        $task->setRunTimePath('./bootstrap/cache/');

        // 添加闭包任务,开2个进程5s执行1次访问网址
        $task->addFunc(function () {
            @file_get_contents('http://xingxinghan.cn/?id=6');
        }, 'Curl_1', 5, 3);

        // 添加执行tp默认控制器的方法,开1个进程10s执行1次访问网址
        //$task->addClass('\app\index\controller\Index', 'index', 'Curl_2', 10, 1);

        // 根据命令执行
        if ($action == 'start')
        {
            $task->start();
        }
        if ($action == 'status')
        {
            $task->status();
        }
        if ($action == 'stop')
        {
            $task->stop();
        }
    }
}

(5).将上面的文件注册一下(修改app/Console/Kernel.php文件)

protected $commands = [
   \App\Console\Commands\Task::class
];

(6).执行命令(windows请使用cmd,由于cmd不支持utf8编码,可以在本站搜索关键字“乱码”相关文章处理),: 

php artisan task start  启动命令
php artisan task status 查询命令
php artisan task stop   关闭命令

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

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

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

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

分享给朋友:

“laravel定时任务,tp定时任务,thinkphp定时任务” 的相关文章

php 获取当前执行文件,php 获取当前执行文件,php 获取当前文件

php 获取当前执行文件,php 获取当前执行文件,php 获取当前文件

php官方的超全局变量$_SERVER['PHP_SELF']也能直接获取,只不过如果url参数太多也会获取。下面提供一个方法获取/*  * 获取当前PHP文件名称  */ if (!function_exists('phpself&#...

php  RabbitMQ消息队列

php RabbitMQ消息队列

(1).config.php 配置文件<?php /**  * RabbitMQ_Config  */ $config = [     'host' => ...

PHP getenv函数和putenv函数的学习

PHP getenv函数和putenv函数的学习

(1).在PHP中可以查看的环境变量包括: (1.1).电脑环境变量 (2.1).服务器环境变量(2).getenv()函数获取一个环境变量的值.参数1是环境变量的key,参数2值为true的时候仅从你的电脑环境变量中查找,参数2值为false会从两种变量中全部查询//获取我电脑登录的用户名,输出A...

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

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

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

mysql悲观锁,mysql乐观锁,mysql乐观锁怎么实现,mysql update 乐观锁

mysql悲观锁,mysql乐观锁,mysql乐观锁怎么实现,mysql update 乐观锁

【一】.无锁场景:多个人购买一件商品,库存减少。商品表:主键   商品名   库存数量 id     name     stock 1&nb...

 php转换文本编码,php字符编码转换,php编码转换

php转换文本编码,php字符编码转换,php编码转换

整理一个自己用的编码转换方法,不需要知道原字符串的编码,只需要传入字符串,和你想要转换的编码。/**  * 编码转换  * @param string $char  * @param string $...