Schedule multiple crons in Laravel

15,769

Solution 1

I think you may need to look at your actual server cron configuration. Laravel scheduler requires the server cron to be run every minute for all schedules to run at the desired time. You may just have the cron running at 8.00am every day instead of every minute.

It may be worth a check, I know I had difficulty with the same issue when I first started scheduling.

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.

Solution 2

Similar situation, except that I am calling a method for each cron using call, for example:

$schedule->call('\App\Http\Controllers\comm\commCron::abc');
$schedule->call('\App\Http\Controllers\comm\commCron::xyz');

I had the same problem, later realised first cron was calling an exit at some point of error.

So please make sure none of the crons retuls in exit or die(). Just return false instead (if you need to).

Share:
15,769
nirvair
Author by

nirvair

Updated on June 13, 2022

Comments

  • nirvair
    nirvair almost 2 years

    I am trying to run multiple jobs in Laravel at different times. This is the code:

    protected function schedule(Schedule $schedule)
    {
        $schedule->command('emails:send')->dailyAt('08:00');
        $schedule->command('emails:evening-status')->dailyAt('17:00');
        $schedule->command('email:weekly-report')->weekly()->mondays()->at('08:00');
    }
    

    The problem here is that only the first command emails:send works. The other commands don't work at the specified time. However, when I run them manually they work fine.

    What is it that I am missing?