记录每天的成长 相信终有一天你能变成你想要成为的人

81 阅读1分钟

laravel使用定时任务

左手牵右手

左手牵右手

程序员

1、生成command文件(没有安装sail扩展的去掉sail)

sail php artisan make:command testCon

2、修改定时任务的名称和描述

3、执行 sail artisan 查看是否注册进去了

4、修改testCon.php handle 代码 加入了while为true 永远为真

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class testCon extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'test:con';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'this is test';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        while (true) {
            echo "测试\r\n"; //小老弟不是打印换行 姐教你
            sleep(5);
        }
        return 0;
    }
}

5、Kernel.php中引入testCon

    protected $commands = [
        Commands\testCon::class,
    ];
 protected function schedule(Schedule $schedule)
    {
        $schedule->command('test:con')->everyMinute();
    }

效果