Magento 2 Cron Job Configuration

Magento 2 comes with a bunch of useful improvements. The enhanced Crone functionality is being on of them.

The platform executes the internal Magento Cron job manager and runs tasks scheduled in the Cron tab. Like in Magento 1, this feature helps store owners set up their own commands that will be running systematically and periodically at a certain time and date.

This functionality is hugely important because many platform's features are based upon periodically scheduled activities. These are:

  • Site Re-indexation
  • Auto-update of the currency rates
  • Generating and re-generating Google sitemaps
  • Private sales (possible only in Magento 2 Enterprise Edition)
  • Managing catalog price rules
  • Managing Magento emails
  • Sending out newsletters
  • and some others

You can run Cron only as the Magento file system owner.

How to Set Up Magento 2 Cron Job

1. Log in to your Magento server

In order to write into the Magento file system, log into your server. You can do this by logging as the owner of the Magento file system.

If you are using the bash shell, you can use this syntax command to login:

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

2. Run the Magento Commands from any directory

Once you have logged into the server, the next thing that you should do is go to your directory where the Magento commands are stored. You can run the commands by adding this syntax:

'your Magento install dir'/bin

to the desired path. Please note that syntax may often vary depending on the shell. Make sure that you look for other references in case the chosen syntax isn't compatible with the shell that 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 like:

 your Magento install dir /bin,


/magento your command name

Another alternative would be:

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

if you are using a docroot web server sub directory in you, go for

'your Magento 2 install dir'

3. Configuring Magento 2 Cron to run at the Background

There are certain requirements to follow before you can run the Magento cron in the background. There are different configurations that can be used in Magenta cron.

The main job of the general Cron is to update the indexes and generate emails and sitemaps which usually runs as PHP line user php.ini along with other two cron jobs that are used by the system upgrades utilities and components managers.

These commands can only be used in the Web servers that are php.ini.

If you are not sure how to run a cron, you can use the commands with the use of the web server configuration.

Web Server Configuration

If you want to know more about your Web server configuration, you can run the file in your browser. You can search a variety of pre-loaded configuration files that are compatible with your Web server.

Once you have the found the right configuration, the next thing that you should configure is the PHP command-line configuration. That can be done by using this syntax php -i | grep php.ini.

After using the said syntax, your results will most likely 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, you can create a Cron job.

The first thing that you should do is to log into as the Admin user, as you must have the root privileges to run this command

crontab -u  -e.

Once you have set the commands, you will see these codes in your screen. The first code updates and reindexes your index, generates the sitemap and automates the emails.

These commands are associated with the PHP command-line in your ini file.

*/1 * * * * php -c    <ini-file-path> <your Magento install dir>/bin/magento cron:run
*/1 * * * * php -c    <ini-file-path> <your Magento install dir>/update/cron.php
*/1 * * * * php -c    <ini-file-path> <your Magento install dir>/bin/magento    setup:cron:run

The other commands below are the Crons for the System upgrade and Component Manager. They are located in the user bin of your Magento 2. After configuring your Web server, make sure that you save all the changes in the crontab, then you are done.

5. Configure Magento Cron 2 for Custom Modules

You can use a cron for your custom module in your Magento. With this cronjob, you can switch groups from one another. You can configure the cron group for your module by using this syntax.

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

then followed by this code in this format:

The <group_name> is the name of  your cron group.

Note that you can only run 1 Cron per group while the  job_name  is the group’s unique ID which is used to identify the cron job.

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

6. Use the Command line to Run the Cron

You can run the cron in your Magento 2 using this command

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

This –groups is used to as a marker to easily identify which of the cron groups should be executed first. You cannot use this command for the default group. This is only applicable for custom module cron groups. Although you can run one cron per group at the same, the cron has to run twice in order to properly execute the given command.

Configuring your Magento Cron is not a rocket science. It makes business easier and faster for retailers and brand owners. If you are not sure on how to configure it on your own, do not hesitate to ask help from professionals. In that way, you will not accidentally misconfigure your other commands.


Submit Comment

* Required Fields