Laravel 命令计划无法正常工作

Laravel Command Schedule Not Working Properly

我正在本地主机上使用 Laravel 7.28 构建一个项目。我需要每小时更新一个 PDF。一开始我创建了一个命令:

<?php

namespace App\Console\Commands;

use App\Event;
use Illuminate\Console\Command;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'All Country PDFs updated';

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

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {

        $event = new Event();
        $event->user_id = 1;
        $event->save();

        echo 'done';
    }
}

它只是在事件中插入一条记录 table 并且工作正常。然后我编辑了App\Console目录下的Kernel.php

<?php

namespace App\Console;

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

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

    /**
     * Define the application's command schedule.
     *
     * @param \Illuminate\Console\Scheduling\Schedule $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('pdf:update')->everyMinute();
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__ . '/Commands');

        require base_path('routes/console.php');
    }
}

最后,我运行php artisan schedule:run。我期待命令 运行s 每分钟一次,但它只 运行s 一次。这是本地主机的问题还是我做错了什么?

php artisan schedule:run 根据其定义只运行一次:

The schedule:run Artisan command will evaluate all of your scheduled tasks and determine if they need to run based on the server's current time.

如果你想每分钟都做一次,你应该使用一个预定的过程控制系统,比如 supervisorcrontab 等等。(more info here)

如果您在 development/local 服务器上使用 laravel 8.x 和 运行,您可以使用以下命令命令,它会为你工作:

php artisan schedule:work

对于 Laravel 7,您还可以在本地

中使用以下命令 运行
while true; do php artisan schedule:run; sleep 60; done

在 windows 本地主机

  1. 首先在运行方法里面设置你想要的调度作业 App\Console\Kernel 有关 Laravel website page here

    的更多信息
  2. 其次在命令行运行下面 命令

php artisan schedule:work