laravel中的定时任务

首先不可避免要是用linux定时任务

* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1

然后就在laravel中进行操作

文件路径app/Console/Kernel.php

<?php

namespace App\Console;

use DB;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel{
    /**
     * 应用提供的Artisan命令
     * 
     * @var array
     */
    protected $commands = [
        // 
    ];

    /**
     * 定义应用的命令调度
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     * @translator laravelacademy.org
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->call(function () {
            DB::table('recent_users')->delete();
        })->daily();
    }
}

下面几种常见的调度方式

$schedule->call(function () {
    // 每周星期一13:00运行一次...
})->weekly()->mondays()->at('13:00');

// 工作日的上午8点到下午5点每小时运行...
$schedule->command('foo')
         ->weekdays()
         ->hourly()
         ->timezone('America/Chicago')
         ->between('8:00', '17:00');

猜你喜欢

转载自blog.csdn.net/liukai6/article/details/83097932