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?
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!
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With