8

I have multiple tasks that need to be done every hour or two. All of them have been scheduled via Laravel using below comamnds as cron jobs

$schedule->command('email:notifications1')
            ->cron('15 * * * * *');
$schedule->command('email:notifications2')
            ->cron('15 * * * * *');
$schedule->command('email:notifications3')
            ->cron('15 * * * * *');

Issue: All of the above tasks are pretty time consuming & it seems from the results that these tasks are not running in parallel. And each tasks runs after the previous has ended.

Requirment How can i run them in parallel? I want all tasks to be executed (in parallel) as soon as the clock tick the specified time.

Laravel Version 5

2 Answers 2

12

You can easily have multiple parallel commands running if you add runInBackground() (introduced in Laravel 5.2) to the chain. Like this:

$schedule->command('email:notifications1')
            ->cron('15 * * * * *')->runInBackground();
$schedule->command('email:notifications2')
            ->cron('15 * * * * *')->runInBackground();
$schedule->command('email:notifications3')
            ->cron('15 * * * * *')->runInBackground();

This creates a new process in the background so the Scheduler doesn't have to wait until the command executes. This doesn't even interfere with the withoutOverlapping() method because that works with mutex files.

Now you also have the benefit of having your commands in version control.

Sign up to request clarification or add additional context in comments.

2 Comments

This is great but not available on Laravel Version 5
@Paranoid Android It was introduced from Laravel 5.2. At the time I answered this question the EOL of 5.1 was well overdue. So I could presume everyone reading this answer was at least using 5.2. You shouldn't be using 5.1 or lower at the moment, that poses a security risk.
8

The Laravel scheduler can only run one command at a time because of the limitations of PHP.

You could, however, add the cronjobs directly in your crontab file, this way, they will be executed in parallel in separate processes.

15 * * * * * php /path/to/artisan email:notifications1
15 * * * * * php /path/to/artisan email:notifications2
15 * * * * * php /path/to/artisan email:notifications3


Another way to fix this is to let the jobs start at another time. Because a new php process is started every minute by the cron job, these do not affect each other.

For example:

$schedule->command('email:notifications1')
            ->cron('5 * * * * *');
$schedule->command('email:notifications2')
            ->cron('10 * * * * *');
$schedule->command('email:notifications3')
            ->cron('15 * * * * *');

3 Comments

The first option is working well for my case. Second option is not working as expected. Thanks for the help.
The first option deprives you the ability to control the versions of the schedule together with the versions of the code
Laravel can run multiple commands at a time if you let the commands run in the background.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.