Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Schedule multiple crons in Laravel

Tags:

php

cron

laravel

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?

like image 247
nirvair Avatar asked Oct 02 '15 17:10

nirvair


People also ask

How do I schedule a cron job in Laravel?

Now to activate the scheduled jobs, run the Cron command. Go to your application from the Applications tab, and then to the Cron Jobs Manager. When done, make sure to save changes. That's it!


3 Answers

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.

like image 79
Sam Killen Avatar answered Sep 21 '22 02:09

Sam Killen


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).

like image 26
Raheel Hasan Avatar answered Sep 20 '22 02:09

Raheel Hasan


It's been 4 years since this question added, but I hope my answer still can help anyone. In my case (adapted from your case), I just added some conditional cases with the Carbon package from Laravel in my Kernel.php.

In my case, I want to run 3 schedulers. Two schedulers will run once every Monday at 8 AM and 5 PM and one else will run once every day. And this is how I make the condition.

public function schedule(Schedule $schedule)
{
   if(Carbon::now()->format('N') == '1')  //check if today is Monday
   {
      if(Carbon::now()->format('G') == '8')  //check if now is 8 AM
      {
         $schedule->command('first:command')->everyMinute();
      }
      if(Carbon::now()->format('G') == '17') //check if now is 5 PM
      {
         $schedule->command('second:command')->everyMinute();
      }
   }

   $schedule->command('another:command')->dailyAt('08:00')->withoutOverlapping();
}

Then, you can set up your cron job in the server to run hourly. The another:command schedule will not run 24 times a day because I have added the withoutOverlapping() method, and it only will run at 8 AM. See this laravel docs about scheduling and this PHP docs about date-time format if necessary.

like image 34
Dani Fadli Avatar answered Sep 23 '22 02:09

Dani Fadli