Run an magento function cron at every hour

Run an magento function cron at every hour

August 17, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Run an magento function cron at every hour

Running a Magento Cron Job Every Hour: A Comprehensive Guide

In Magento, cron jobs are utilized for scheduling regular tasks like inventory management, sending emails, or updating data. If you want to run a specific function every hour, you can achieve this by modifying the config.xml file and ensuring the cron is set up correctly.

The Problem with Your Current Setup

The provided code seems to be missing some essential elements for a Magento cron job configuration:

  1. Namespace: The <crontab> element should be nested within <global> tags.
  2. Cron Expression: While the expression **/1 * * * is correct for running the job every hour, it needs to be enclosed in quotes.
  3. Model Class: The model class stockalert/observer::backinstock should follow the Magento naming convention: StockAlert\Observer::backinstock.

The Correct Configuration

To create a cron job that runs a specific function every hour, update your config.xml file as follows:

<global>
    <crontab>
        <jobs>
            <stockalert_hourly_job>
                <schedule><cron_expr>"0 * * * *"</cron_expr></schedule>
                <run><model>StockAlert\Observer::backinstock</model></run>
            </stockalert_hourly_job>
        </jobs>
    </crontab>
</global>

Here’s what each part of the configuration means:

  • Job Name: stockalert_hourly_job - A unique identifier for your cron job.
  • Schedule: <cron_expr>"0 * * * *"</cron_expr> - This expression tells Magento to run the job on every hour (0 minute of every hour).
  • Model Class: <model>StockAlert\Observer::backinstock</model> - The model class and method that should be executed.

Ensuring Your Cron Job Runs

After updating the config.xml file, you need to make sure the cron job is actually run by Magento. You can do this in one of two ways:

  1. Manually Run: Navigate to your Magento admin panel (admin.example.com/admin) and go to System -> Configuration -> Advanced -> Cron Jobs. Click “Select All” and then “Run Jobs”.

  2. Scheduled Command: You can schedule a command-line script to run the cron jobs at regular intervals using a task scheduler like Cron on Unix-based systems or Task Scheduler on Windows.

Conclusion

In Magento, creating an hourly cron job involves properly configuring the config.xml file and ensuring the cron is executed regularly. The key takeaways are:

  • Follow the correct naming conventions for model classes.
  • Use the appropriate cron expression (0 * * * * for every hour).
  • Ensure you have a mechanism to run the cron jobs, either manually or via scheduled command.

Remember, always test your cron jobs thoroughly in a staging environment before deploying them to production. For more detailed information on Magento cron jobs and best practices, visit our blog at Laravel Company.

Extensions Configuration Cron Email

Our network