Laravel5:如果没有准备好运行命令,如何禁用默认调度程序消息

lin*_*lin 8 php scheduler laravel-5

使用Laravel5调度程序时:

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

如果没有准备好运行命令,我们会收到以下默认输出:

# No scheduled commands are ready to run.
Run Code Online (Sandbox Code Playgroud)

如何禁用此默认Laravel5消息?如果没有准备好运行的命令,我们不希望有输出.当我们能够配置该消息并返回我们自己的代码时,最好的是.

Dav*_*e S 3

您可以创建一个app/Console/Commands类似于下面的新命令,它扩展了默认schedule:run命令。

它会覆盖该handle方法,同时保留其他所有内容不变,以避免 Laravel 输出“没有计划的命令已准备好运行”。当它没有做任何事情时就行。

通过使用不同的名称,无需担心冲突,并且php artisan schedule:run如果您愿意,您仍然可以随时运行原始命令。

<?php

namespace App\Console\Commands

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Console\Scheduling\ScheduleRunCommand;

class RunTasks extends ScheduleRunCommand
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'run:tasks';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Custom task runner with no default output';

    /**
     * Create a new command instance.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    public function __construct(Schedule $schedule)
    {
        parent::__construct($schedule);
    }

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function handle()
    {
        foreach ($this->schedule->dueEvents($this->laravel) as $event) {
            if (! $event->filtersPass($this->laravel)) {
                continue;
            }
            if ($event->onOneServer) {
                $this->runSingleServerEvent($event);
            } else {
                $this->runEvent($event);
            }
            $this->eventsRan = true;
        }

        if (! $this->eventsRan) {
            // Laravel would output the default text here. You can remove
            // this if statement entirely if you don't want output.
            //
            // Alternatively, define some custom output with:
            // $this->info("My custom 'nothing ran' message");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

验证 Laravel 是否看到您的新命令:

php artisan | grep run:tasks
Run Code Online (Sandbox Code Playgroud)

最后更新你的 cron 以运行新命令:

* * * * * cd /path-to-your-project && php artisan run:tasks >> /dev/null 2>&1
Run Code Online (Sandbox Code Playgroud)