Magento: Setup cronjob every 2 hours

Magento: Setup cronjob every 2 hours

July 26, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: Setup cronjob every 2 hours

Magento: Scheduling Cron Jobs Every 2 Hours

If you’re facing issues scheduling cron jobs every 2 hours in your Magento setup, this guide will help you resolve the problem efficiently.

Understanding Magento’s Cron Job Format

Magento uses a cron expression format to schedule tasks. This format is defined by the IETF standard with a specific syntax:

<minute> <hour> <day_of_month> <month> <day_of_week>

In your provided XML configuration, you’ve used 0 */2 * * *, which means “run this job every 2 hours”.

Why Isn’t Your Cron Job Being Scheduled?

The issue lies in the interpretation of the cron expression. Magento’s cron.php script uses a PHP library called SimpleCron to parse and validate the expressions.

Unfortunately, SimpleCron doesn’t support the */n syntax for hourly intervals. This is why changing */2 to * (which means “every hour”) resolves your issue. However, this solution isn’t ideal as it would run the job every single hour instead of every 2 hours.

A More Elegant Solution

To schedule a cron job every 2 hours without resorting to the inelegant string of numbers you mentioned earlier, we can use SimpleCron’s every syntax along with the hourly interval:

0 0/2 * * *

Let me break this down:

  • 0: Run at minute 0 (i.e., on the hour)
  • 0/2: Run every hour, starting from the first occurrence of the hour
  • * *: Every day and month
  • *: Any weekday

Implementing This Solution in Magento

To apply this solution to your XML configuration:

<jobs>
    <stockupdate>
       <schedule><cron_expr>0 0/2 * * *</cron_expr></schedule>
        <run><model>stockupdate/cron::start</model></run>
    </stockupdate>
</jobs>

Testing the Cron Job

To test if your cron job has been scheduled:

  1. First, truncate the cron_schedule table: TRUNCATE cron_schedule.
  2. Clear the cache by running rm -rf var/cache.
  3. Run the cron.php script with php cron.php.
  4. Check the contents of the cron_schedule table to see if your job has been scheduled:
SELECT * FROM cron_schedule;

Conclusion

In summary, when scheduling cron jobs every 2 hours in Magento, you need to use SimpleCron’s syntax correctly.

The solution is to use the 0 0/2 * * * expression in your cron configuration. This tells Magento to run the job at minute 0 of every hour that ends with an even number (i.e., every 2 hours).

Remember, after making changes to your configuration, always clear the cache and run the cron.php script to ensure the jobs are scheduled correctly.

For more advanced Magento configurations and best practices, I recommend checking out the official documentation or visiting Laravel Company for expert insights.

Configuration Cron Pricing Errors

Our network