Laravel 计划在初始化时仅触发一次

Laravel schedule fire only once on initialization

当我使用 php artisan schedule:run 命令时,shuffleQuoteOfTheDay 方法被评估一次,然后它不会每分钟触发一次。

Kernel@schedule 方法

protected function schedule(Schedule $schedule)
{

  $schedule->call('App\Services\MotivationalQuoteService@shuffleQuoteOfTheDay')->everyMinute();

}

MotivationalQuoteService@shuffleQuoteOfTheDay 方法

  public function shuffleQuoteOfTheDay(){

    $currentQuoteOfADay = $this->getQuoteOfTheDay();

    $randomQuote = MotivationalQuote::where('quote_of_the_day',false)->inRandomOrder()->first();

    $currentQuoteOfADay->update(['quote_of_the_day' => false]);

    $randomQuote->update(['quote_of_the_day' => true]);

    return $randomQuote;

  }

它只工作一次但不是每分钟,我创造了工作机会table.Can有人帮我吗?提前谢谢你。

您需要设置 cron 以便它可以每分钟 运行 php artisan schedule:run 命令。

来自the docs

When using the scheduler, you only need to add the following Cron entry to your server. If you do not know how to add Cron entries to your server, consider using a service such as Laravel Forge which can manage the Cron entries for you:

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

This Cron will call the Laravel command scheduler every minute. When the schedule:run command is executed, Laravel will evaluate your scheduled tasks and runs the tasks that are due.

Task Scheduling:

这是您需要添加到服务器的唯一 Cron 条目:

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

此 Cron 将每分钟调用 Laravel 命令调度程序。然后,Laravel 评估您的计划任务和 运行 到期的任务。

Laravel的任务调度器不驻留在内存中,需要每分钟运行。然后它将检查在那一分钟内哪些任务需要 运行 并 运行 它们。当您 运行 使用 PHP 任务调度程序时,它只会 运行 一次,它需要 cron 每分钟 运行 它。

如果您的开发机器是 linux machine/homestead 那么您可以用 lcoally 测试它。

要添加一个 cron 作业:

$ crontab -e

然后在crontab文件中添加* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1并保存

不要忘记start/reload cron 服务。

上 ubuntu:

 $  sudo service cron reload