Setup a Magento cron job to call multiple models

Setup a Magento cron job to call multiple models

October 17, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Setup a Magento cron job to call multiple models

Running Multiple Models from a Single Cron Job in Magento

As an experienced developer, I often encounter situations where multiple models need to be executed simultaneously within a cron job in Magento. The default configuration might lead you to believe that running two models from one cron schedule isn’t possible. However, with the right approach and understanding of Magento’s cron job structure, it can indeed be achieved.

Understanding Magento Cron Job Structure

In Magento, cron jobs are defined in the config.xml file within your custom module or extension. The basic syntax for defining a cron job looks like this:

<crontab>
    <jobs>
        <Model_CronJob>
            <schedule>
                <cron_expr>* * * * *</cron_expr>
            </schedule>
            <run>
                <model>Model_CronJob::execute</model>
            </run>
        </Model_CronJob>
    </jobs>
</crontab>

This configuration tells Magento to run the execute method of Model_CronJob every minute (because * * * * * is a cron expression for “every minute”).

Running Multiple Models from One Cron Job

Now, let’s address your specific question. You’re trying to run both Foo::cronJob and Bar::cronJob models within the same cron schedule. The approach you’ve tried is almost correct, but it needs a slight adjustment.

Instead of defining multiple <model> tags in the <run> section, which is causing only one model to actually execute, we should create a separate model that acts as an intermediary and calls both Foo::cronJob and Bar::cronJob.

First, define your new intermediary model:

class MyIntermediateModel extends Mage_Core_Model_Abstract
{
    public function runMultipleModels()
    {
        $foo = new Class_Module_Foo();
        $foo->cronJob();

        $bar = new Class_Module_Bar();
        $bar->cronJob();
    }
}

Then, update your config.xml to reflect this change:

<crontab>
    <jobs>
        <Class_Module_Multiple>
            <schedule>
                <cron_expr>* * * * *</cron_expr>
            </schedule>
            <run>
                <model>Class_Module_Multiple::runMultipleModels</model>
            </run>
        </Class_Module_Multiple>
    </jobs>
</crontab>

The key here is to create a new model (MyIntermediateModel) that calls both Foo::cronJob and Bar::cronJob. This approach ensures that both models are executed within the same cron job.

Best Practices for Magento Cron Jobs

While this solution works, it’s important to keep some best practices in mind:

  1. Use a Separate Cron Job: If your tasks are complex or time-consuming, consider breaking them into separate cron jobs with different schedules. This prevents any single task from monopolizing the cron process.

  2. Handle Exceptions Gracefully: Always wrap your cron job methods in try-catch blocks to handle exceptions and prevent the entire cron process from failing due to a single error.

  3. Monitor Performance: Keep an eye on the performance of your cron jobs, especially if you’re executing them frequently or handling large amounts of data. Slow cron jobs can impact other parts of your site’s functionality.

Conclusion

Running multiple models from a single cron job in Magento is absolutely possible with the right approach. By creating an intermediary model and updating your config.xml accordingly, you can ensure that all necessary models are executed within the same scheduled task.

Remember, while this solution works for simple cases, more complex scenarios might require a deeper understanding of Magento’s cron job architecture and possibly even custom cron handlers.

For further reading on Magento cron jobs and best practices, I recommend checking out this comprehensive guide to Magento cron jobs from Laravel Company.

Extensions Configuration Cron Pricing

Our network