Skip to content

How To Set Cron Job in Magento 2

Cron is a critical aspect of running scheduled tasks in Magento. This feature helps Magento store owners to set up their own commands to run systematically at a specific time and date. Many activities get impacted by the cron functionality as listed below:

  • Managing Magento emails

  • Sending out newsletters

  • Site Re-indexation

  • Auto-update of the currency rates

  • Generating and re-generating Google sitemaps

  • Private sales

  • Managing catalog price rules

Run your corn job as a Magento File System owner.

How to Set Up Magento 2 Cron Job

1. Log in to your Magento server

To write into the Magento file system, you must log in to your server as the owner. Or you can use the given syntax if you are using the bash shell.

su 'Magento 2 file system owner' -s /bin/bash -c 'command'

2. Run the Magento Commands from any directory

As you log into the server, go to the directory where the Magento commands are stored and run the commands by adding the syntax to the desired path.

'your Magento install dir'/bin

The syntax may vary depending on the shell, so look for the other references in case the chosen syntax is incompatible with the shell you are using.

For base shells using CentOS, you can use this syntax:

export PATH=$PATH:/var/www/html/magento2/bin

You can also use some other commands as:

your Magento install dir /bin,

or

/magento your command name

Another option would be:

php 'your Magento 2 install dir' /bin/magento 

If you are using a docroot web server sub-directory, go for:

'your Magento 2 install dir'

3. Configuring Magento 2 Cron to run at the Background

There are specific requirements to follow before running the from jobs in the Background and different configurations that need to be done for Cron.

The primary purpose of the general Cron is to update the indexes and generate emails and sitemaps. This runs typically as PHP line user php.ini along with two other corn jobs for the system upgrades utilities and components managers.

So these commands are helpful in the web server that is php.ini. If you are uncertain about how to run a cron, you can use the commands with the help of the web server configuration .

As you have found the correct configuration, the next thing you need to perform is to configure the PHP command line. It can be done using the syntax php -i | grep php.ini.

Once done, your results should look like this:

Configuration File (php.ini) Path =>  /etc/php5/cli
Loaded Configuration File =>  /etc/php5/cli/php.ini

4. Creating Magento 2 cron job

Now, let's create a cron job. Log in as the Admin user, as you must have the root privileges to run the given command.

crontab -u  -e.

You can see these codes on your screen as you set the commands. The first code updates and reindexes the index and generates the sitemap and emails.

The listed commands are related to the PHP command line in your ini file .

1
2
3
*/1 * * * * php -c     /bin/magento cron:run
*/1 * * * * php -c     /update/cron.php
*/1 * * * * php -c     /bin/magento    setup:cron:run

Below mentioned commands are the Crons for the system upgrade and component manager. They are located in the user bin of Magento 2. After successful configuration with your web server, make sure to save all the changes in the crontab, and you are done!

5. Configure Magento Cron 2 for Custom Modules

Even, Cron can be used for the custom module too. You can change the groups from one to the other with this cronjob. To configure the cron group for the custom module, use this syntax:

create 'your magento2 component base dir'/'vendorname'/module-'name'/etc/crontab.xml 

then followed by this code in this format:

<config>

<group id="<group_name>">

<job name="<job_name>" instance="<classpath>" method="<method>">
<schedule><time></schedule>
</job>

</group>

</config>

This is the name of your cron group.

You can run 1 Cron per group while the job_name is the unique group ID to identify the job.

< classpath > is the identity name of the cron job. The code is used to indicate the job of the classpath while is the time when the cron job should be executed.

6. Use the Command line to Run the Cron

You can use the command to run cron in Magento 2.

magento cron:run [--group=""]

This -gropus is used to identify whicht of the cron groups should be executed first. It cannot be used for the default group and only applicable to the custom module cron groups. Eventhogh, you run one cron per group, the cron has to run twice in order to properly execute the given command.

By proper understanding for your business needs, you can schedule your cron jobs that runs the business faster! If you are unsure of how to manage and schedule the cron, don't hesitate to ask for expert help!


Last update: 2022-06-28