How to Set Up and Run Magento Cron Jobs?

How to Set Up and Run Magento Cron Jobs?

The Magento cron job automates tasks like reindexing, email sending, catalog pricing updates, and currency rate updates. Cron Jobs are ideal for repetitive or scheduled tasks needing daily or weekly project execution. This tutorial helps you create and configure Magento cron jobs.

Key Takeaways

  • Learn how to install and configure cron jobs in Magento 2 for automated tasks.

  • Discover two ways to set up your cron schedule: through the command line or Magento admin panel.

  • Understand how to manually run cron jobs and tailor them to specific groups for targeted execution.

  • Get answers to FAQs about Magento cron jobs, including how often they should run and who should manage them.

How to Install Cron in Magento 2

1. To access your server, login and execute the cron: install command

To begin, log in to your Magento server using the credentials of the filesystem owner. Once logged in, navigate to the Magento root directory and execute the specified command.


$ bin/magento cron: install

2. Do verification of the Magento crontab

To check the crontab, use this command:


$ crontab -l

The output will look like this:

Command prompt displaying Magento 2 cron installation process with "$ crontab -l" command.

Installation of Magento 2 cron is complete.

Steps for Running Magento 2 Cron Manually

To manually run cron jobs in the Magento 2 server, follow these steps:

  • Log in to your Magento server via SSH.

  • Go to the project root.

  • Execute the following command as the Magento file system owner:


$ bin/magento cron:run

  • Run the command twice: once to find cron tasks and again to execute them. You also have the choice to run Magento cron for a specific group if desired:

$ bin/magento cron:run --group

To run cron tasks for specific groups, replace the <group-name> placeholder with a cron group name like "index" or "default."

Configure Magento 2 Cron Job Schedule

You can do cron configuration for Magento store in two ways:

  • Use the command line.

  • Access the admin panel.

1. Configure Cron Jobs in Magento Using the Magento CLI

To access your Magento web server, log in as the filesystem owner and go to the Magento project root. You need to check the current crontab schedule. Use the following command to do this:


$ crontab -l


The default cron interval in Magento is set to * * * * *, but you have the option to customize it. To run cron less frequently, replace the default entry with */5 * * * * for a five-minute interval.


To create a custom cron job for Magento 2, you can utilize an online cron expression editor such as crontab guru or Cronhub. Afterward, you can edit the crontab using a specific command:


$ crontab -e


If you're new to using the crontab edit command, you'll see the following prompt on your screen:

Command prompt demonstrating configuration of Magento 2 cron job schedule with &quot;$ crontab -e&quot; command.

Press Enter to continue using the default text editor like Nano. Alternatively, you can select your preferred editor by entering the corresponding number.


To configure the Magento cron job, locate the code between the #~ MAGENTO START and #~ MAGENTO END comments. Replace the default values with your desired expression. Save the file and exit the text editor.


The Magento cron will now follow the newly set schedule in the cron tab.

2. Steps to Customize Magento Cron From the Magento Admin

Magento 2 admin panel interface showing steps to customize Magento cron jobs.

To access the Magento admin panel, go to Stores > Settings > Configuration. In the left panel, expand the Advanced setting option and select System.

Then, to expand the Cron (Scheduled Tasks) section, click the "Use system value" checkbox. This will update the values for each group.

After configuring cron jobs to run at your desired intervals, click Save Config.

FAQs

1. What is the command to create a cron job in Magento?

You can create a cron job in Magento by running the following command in your terminal: bin/magento cron:install. A file system owner should run this with write permissions for the Magento file system.


2. How do I configure Magento cron jobs?

To set up cron jobs in your Magento 2 admin panel, navigate to the cron configuration section. Create a new group and configure the cron jobs accordingly. Save your changes and ensure the new group is functioning properly.


Staying informed about the latest Magento updates and industry best practices is crucial to maintaining a well-optimized cron job configuration. Regularly checking for Magento updates and adjusting your cron schedule ensures that your online store runs efficiently. You may also get help from the official Magento cron configuration documentation.


3. Can I create custom cron jobs in Magento?

Yes, Magento allows you to create custom cron jobs by scheduling specific tasks in the Cron (Scheduled Tasks) section of the Magento admin panel. You can define the interval at which these tasks should be run.


4. How frequently should Magento cron jobs run?

Magento recommends that cron jobs run every minute for optimal performance. You can configure this in your server's crontab file. A robust and dedicated Magento hosting platform can ensure optimal performance for your Magento 2 Cron Jobs.


5. What does the file system owner do about Magento's cron?

The file system owner of the Magento application should be the one to set up and manage cron jobs. This is because they will have the necessary permissions to modify the Magento file system and run commands.


6. How do I use the Magento 2 store cron module?

Once you install Magento 2, the store cron module can be accessed from the admin panel. You can configure and manage cron jobs within this module to automate repetitive tasks.


7. What is the purpose of the web server's cron.php in Magento?

The web server's cron.php file in Magento is used to execute cron jobs. The server's crontab triggers this file at specified intervals, and it, in turn, triggers Magento's cron tasks.

Summary

Magento cron job configuration allows you to schedule and automate repetitive tasks. It lets your online store run smoothly without manual intervention.


This guide covered everything about how to create cron jobs in Magento. It empowers you with the knowledge to optimize your ecommerce operations. Magento hosting plans provide access to optimized servers designed for Magento.

Shivendra Tiwari
Shivendra Tiwari
Technical Writer

Shivendra has over ten years of experience creating compelling content on Magento-related topics. With a focus on the Magento community, he shares valuable tips and up-to-date trends that provide actionable insights.


Get the fastest Magento Hosting! Get Started