Home > database >  Configuration task scheluder in Laravel (cron)
Configuration task scheluder in Laravel (cron)

Time:07-11

I make my first cron in Laravel. I use Laravel 9 in my project. I have command, which I need run every sundays and saturdays MIDNIGHT.

I write this code:

class Kernel extends ConsoleKernel
{
    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('recycler:update')
            ->sundays()
            ->saturdays()
            ->hourly();
    }

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

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

is this correct configuration?

When I run command from artisan: php artisan recycler:update - it's work fine. Now I need run this from cron :)

CodePudding user response:

I would try set it this way :

$schedule->command('recycler:update')
        ->weekends()
        ->at(00:00);

As the weekends() method schedules the event to run only on saturdays and sundays. Besides, you want to run it only at midnight and not every hours.

You can then check your schedules tasks runnning :

php artisan schedule:list
  • Related