Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel5: How to disable default scheduler message if no command is ready to run

When using the Laravel5 scheduler:

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

We receive the following default output if no command is ready to run:

# No scheduled commands are ready to run.

How to disable this default Laravel5 message? We don't want to have an output if there is no command ready to run. The best would be, when we were able to configure that message and return code on our self.

like image 688
lin Avatar asked Feb 14 '19 09:02

lin


People also ask

How do I stop a scheduler in Laravel?

php. $schedule->command('myCommand:doesStuff')->hourly()->runInBackground();


2 Answers

You can create a new command in app/Console/Commands similar to below, which extends the default schedule:run command.

It overrides the handle method while leaving everything else as-is to avoid having Laravel output the "No scheduled commands are ready to run." line when it didn't do anything.

By using a different name there's no need to worry about conflicts, and you can still run the original php artisan schedule:run command at any time if you so desire.

<?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");
        }
    }
}

Verify that Laravel sees your new command:

php artisan | grep run:tasks

Finally update your cron to run the new command:

* * * * * cd /path-to-your-project && php artisan run:tasks >> /dev/null 2>&1
like image 189
Dave S Avatar answered Oct 19 '22 01:10

Dave S


As I mentioned in the comments I see two possibilities

You can filter the output by removing what you don't want

* * * * * cd /path-to-your-project && php artisan schedule:run | awk '{ if (/No scheduled commands are ready to run./ && !seen) { seen = 1 } else print }'

Or you can override with your own command:

$ php artisan make:command ScheduleRunCommand

By making your own command (mostly copy/past from ScheduleRunCommand) or extending the ScheduleRunCommand as @dave-s proposed

And if you want to still run php artisan schedule:run with your new command, you need to register it in a service provider

$this->app->extend('schedule.run', function () {
    return new \App\Console\Commands\ScheduleRunCommand;
});
like image 42
Clément Baconnier Avatar answered Oct 19 '22 00:10

Clément Baconnier