laravel调度任务

时间:2022-04-20 11:23:32

<?php

namespace App\Console;

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

class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
\App\Console\Commands\Inspire::class,
];

/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{

$schedule->exec(
$schedule->call(function () {
DB::table('ceshi')->insert(['contents'=>'新的数据']);
})->everyMinute()
)->daily();
}
}

执行调度任务,添加Crontab定时任务,
用命令crontab -e 添加如下内容

* * * * * /usr/local/bin/php /usr/local/var/www/projectName/artisan schedule:run >> /dev/null 2>&1
  • 1
  • 2

如图:
laravel调度任务
上面命令Crontab会每分钟去调Laravel的schedule命令,然后Laravel判断执行任务。