Magento cron job: what it is and how to configure correctly

BuiltWith usage statistics show that Magento ranks second in the list of ecommerce platforms used by the top 10,000 ecommerce websites globally. This is because Magento can deliver industry-leading functionality without compromising on performance or a hefty price tag.

When you’re setting up a Magento 2 store, one of the first tasks that must be carried out after installing the application is configuring the Magento cron job. As of version 2.2, Adobe has simplified the process of setting up cron jobs for Magento. They even provide detailed documentation allowing developers to create custom cron jobs and cron groups when carrying out Magento extension development.

Although the process of setting up the cron jobs might seem easy, if configured incorrectly, it can lead to slow- and long-running crons that can severely degrade a website’s performance. But before getting into the nitty-gritty of Magento cron, let’s go over the basics of cron.

What is cron?

Cron, or a cron job, is a time-based task scheduler in Unix systems that allows system administrators to set up and maintain application environments by scheduling tasks such as commands or shell scripts that must be executed regularly.

Perfect for automating repetitive tasks, cron’s general-purpose nature makes it the ideal tool for the Magento application to perform frequent activities on recurring schedules. All actions are defined using shell commands in a configuration file called the crontab.

Inside crontabs, cron jobs are scheduled using the cron time string consisting of five fields that are converted to time intervals to determine the frequency of executing each task. The standard cron time formatting consists of five values separated by spaces.

The acceptable value for each character is as follows:

Character

Descriptor

Acceptable values

1

Minute

0 to 59, or * (no specific value)

2

Hour

0 to 23, or * for any value. All times UTC.

3

Day of the month

1 to 31, or * (no specific value)

4

Month

1 to 12, or * (no specific value)

5

Day of the week

0 to 7 (both 0 and 7 represent Sunday), or * (no specific value)

For example, if you wish to run a task every Monday at 9 pm, you would set the cron schedule 0 9 * * 1.

In all crontabs, there must be an acceptable value set for each character entry. You can also use a forward slash (/) followed by a number in any character position to run the task at a regular interval independent of the standard time.

For example, to execute a command every five minutes, you would enter the cron time as */5 * * * *.

What is a Magento cron job?

A cron job in Magento is responsible for performing store operations such as:

  • Generating the sitemap,
  • Updating currency rates,
  • Reindexing,
  • Generating emails,
  • Updating catalog price rules.

A Magento cron job can be run either using:

  1. The magento cron:run command from the command line.
  2. By accessing the pub/cron.php file in a web browser.

Using the magento cron:run command is preferred as it automatically uses a secure process compared to the latter, which needs to be secured before use.

1. Using the magento cron:run command

Magento allows running the Magento cron job using the magento cron:run command in two ways:

  1. From the command line,
  2. In a crontab.

A. From the command line

Using the command line, you can run the Magento cron job by executing the following command as the Magento filesystem owner:

<code>

$ bin/magento cron:run [–group=”<cron group name>”]

</code>

You can specify the cron group using the ‘–group’ flag or skip it entirely if you’d like to run the cron for all groups.

For example, to run the indexing cron job in Magento, you can run the following command in your terminal:

<code>

$ bin/magento cron:run –group index

</code>

However, as these tasks must run periodically, running the Magento cron job from the command line isn’t efficient. Using a crontab, this process can be automated, freeing system administrators to focus on more pressing tasks while allowing the store to function smoothly.

Let’s look at how you can set up a Magento cron job in a crontab.

B. In a crontab

The configuration file containing the Magento cron job is called the Magento crontab. Inside this file, you can configure the cron to run on a schedule by specifying a time string and the command to be executed.

Because Magento relies heavily on cron to perform vital store-related tasks, running it from a crontab allows merchants to automate the execution of the tasks on a schedule.

To install the Magento cron job in a crontab, log in to your server as the Magento filesystem owner. Next, navigate to the Magento root directory and execute the following command.

<code>

$ bin/magento cron:install

</code>

This command will write the Magento crontab in the Magento filesystem owners crontab.

To view the created Magento crontab, run the following command in your terminal:

<code>

$ crontab -l

</code>

You should receive an output similar to:

<code>

#~ MAGENTO START c5f9e5ed71cceaabc4d4fd9b3e827a2b

* * * * * /usr/bin/php <path-to-magento-root-directory>/bin/magento cron:run 2>&1 | grep -v “Ran jobs by schedule” >> < path-to-magento-root-directory>/var/log/magento.cron.log

#~ MAGENTO END c5f9e5ed71cceaabc4d4fd9b3e827a2b

</code>

The Magento crontab is always placed inside the #~ MAGENTO START and #~ MAGENTO END comments. It’s worth noting that running the ‘magento cron:install’ command will not overwrite any cron jobs that may already exist inside the Magento comments.

If you’ve already created a crontab for your Magento installation and wish to overwrite it with the default Magento crontab, run the following command:

<code>

$ bin/magento cron:install –force

</code>

Using the ‘–force’ flag will rewrite any existing crontabs within the Magento comments without overwriting any that exist outside of it.

The default schedule of the Magento cron job is * * * * *, which means that the cron jobs are set to run every minute. You can customize this schedule from within the terminal and even through the Magento admin panel.

2. Through a web browser

You can also run the Magento cron job from a web browser. However, this method requires additional configuration to secure the cron.php file before accessing it via the browser. This is because an unsecured cron.php file can allow any user to attack your Magento store with ease.

By default, Magento restricts accessing the file in a browser via the .htaccess file in the root directory. This is a security precaution to defend your store from malicious exploits. Generally, running the Magento cron job using this method is not recommended and is best left to experienced Magento developers when customizing a store in a development environment.

Scheduling Magento cron job from the command line

To configure the cron job for your Magento installation through the command line, make sure you’re logged into your server as the Magento filesystem owner. Next, view the existing crontab for the user by running the following command:

<code>

$ crontab -l

</code>

If you’re using the default Magento crontab, the cron schedule will be configured to run every minute, i.e., * * * * *. You can replace this by writing your schedules or using an online cron expression editor such as crontab guru to generate the cron schedule and even to verify the schedule you may have created.

To edit the default crontab, run the following command as the Magento filesystem owner:

<code>

$ crontab -e

</code>

If it’s your first time running the crontab edit command, you will be prompted to select a default text editor as follows:

<code>

Output

no crontab for magento_user – using an empty one

Select an editor.  To change later, run ‘select-editor’.

  1. /bin/nano        <—- easiest
  2. /usr/bin/vim.basic
  3. /usr/bin/vim.tiny
  4. /bin/ed

Choose 1-4 [1]:

</code>

You can press ENTER and select the default nano text editor or enter the number corresponding to your preferred editor.

Once inside the editor, navigate your cursor to Magento’s crontab schedule within the Magento start and end comments and replace the default values with your custom schedule. 

For example, to run the cron job every five minutes, instead of every minute, replace the default * * * * * with */5 * * * *.

Finally, save the file and exit the editor. Your Magento crontab will perform tasks based on your custom schedule now.

Configuring Magento cron job from the admin panel

The Magento admin panel also allows you to control other settings related to the cron jobs, such as how long the cron execution history is maintained and if different cron groups must be run in separate processes. 

You can configure and manage the Magento cron inside the admin panel by logging into your store’s admin panel as the store administrator. Navigate to Stores > Settings > Configurations and in the left side panel, expand the ‘Advanced’ section and choose ‘System’.

Then, expand the ‘Cron (Scheduled Tasks)’ section and update the settings under the ‘Index’ and ‘Default’ groups as required. Here’s a brief description of each setting under the cron groups:

Setting

Description

Generate Schedules Every

How often schedules are generated and stored in the database. (in minutes)

Schedule Ahead for

How far in advance cron jobs must be scheduled. (in minutes)

Missed if Not Run Within

Set in minutes the time after which the status of a failed cron job will be set to ‘Missed’. (in minutes)

History Cleanup Every

Time after which history of cron jobs with ‘ended’ status will be cleared from the database. (in minutes)

Success History Lifetime

Time after which the history of cron jobs with ‘Successful’ status remains in the database. (in minutes)

Failure History Lifetime

Time after which the history of cron jobs with ‘Error’ status remains in the database. (in minutes)

Use Separate Process

Set whether all cron jobs from the group are run in a separate system process.

Options: Yes / No

Once you’ve updated the settings to your preferred values, make sure you click ‘Save Config’ to save your changes.

Wrapping it up

The Magento cron job simplifies the process of running an ecommerce store by automating background tasks. Configured correctly, the Magento cron job will ensure your store operates smoothly and will help you to leverage all functionality in Magento without losing out of performance.

Author: Jan Guardian

 

Jan is the Chief Business Development Officer at Staylime, a Magento design and development company headquartered in Redwood City, California. He is a Member of the Magento Association and an Adobe Sales Accredited Magento Commerce professional.

Jan is passionate about ecommerce and Magento in particular — throughout the years his articles have been featured on Retail Dive, Hacker Noon, Chief Marketer, Mobile Marketer, TMCnet, and many others.