Configure And Run Cron Job in Magento 2

by Cloudways  February 1, 2016

Magento 2 comes with many awesome features. In Magento 1 & 2, Cron job is one of the most stunning feature. Cron helps Magento 2 store owners to setup commands which will run systematically at a set time and date.  In Magento, Cron jobs run scheduled tasks, reindexing, generating e-mails, generating newsletters, sitemaps and more. Basically the cron executes internal Magento cronjob manager and manages which cron jobs are executed and when.

Magento 2 Cron

Cron is a vital part of Magento. Cron configuration in Magento 2  is highly important because many Magento features are based on scheduled activities, possible only via cron jobs. Here is the list of some of them:

  • Reindexing
  • Private sales (Magento Enterprise Edition)
  • Automatic update of currency rates
  • Magento emails
  • Customers alerts and notifications
  • Generating Google sitemaps
  • Catalog price rules
  • Newsletters

Run cron as the Magento file system owner. Do not run cron as root. Running cron as the web server user is not a good practice. It is also required to mention that in UNIX systems, scheduled tasks depend on crontab.

First Step of Magento 2 Cron Configuration:

Before configuration on Magento 2 cron, it is required to perform some important tasks.

  • Login to the Magento 2 server as a master user, who has permissions to write to the Magento 2 file system. If you use the bash shell, you can use the following syntax to switch to the Magento file system owner and use following command:

  • For Running Magento 2 commands from any directory add <You Magento 2 install dir>/bin to your system path. Bash shell example for CentOS:

There are other ways to run commands as well:

In case of

You can run this command as

./magento <your command name>

Alternatively,

Where <your Magento 2 install dir> is a web servers docroot sub directory.

Run cron in the background For Magento 2 Cron Configuration:

Here we discuss how to run all Magento 2 cron jobs every minute, which is the recommended for both Magento CE and EE edition.

Requirements For Running Cron in  background

Magento cron is run with different configuration. General cron job that reindexes, generates emails and sitemap usually runs as the PHP command line users php.ini and two others cron  jobs are used by the component manager and system upgrade utilities. These commands must be used in the web servers php.ini

If you don’t have experienced with running cron. You can use all commands with the web server’s configuration.

Web Server Configuration

Find out web server configuration, run phpinfo.php file in your web browser and search the loaded configuration file option.

PHP command-line

After web server configuration, PHP command-line configuration is required. Use these commands for command-line configuration:

As result you get something like this:

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

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

Creating Magento 2 cron job

To create a cron job, you must login as a user with root privileges (as I mentioned before) and run following command:

“For example” crontab -u magento_cloudways -e

Use the following commands, but you may need to choose a text editor first.

First command reindexes indexers, sends automated e-mails and generates the sitemap. Basically this command is associated with the PHP command line .ini file. Other two commands are used for the Component Manager and System Upgrade e.g. if the PHP is located in /user/bin you installed Magento 2 in /var/www/html/magento2 and all commands used the server’s php.ini.

Example:

Now save your all changes to the crontab.

Configure Magento 2 Cron Groups For Custom Module

If you wish to implement cron for a custom module Magento, it allows you to use the default group or different group. For configuration of cron group for your module, create <your magento2 component base dir>/<vendorname>/module-<name>/etc/crontab.xml with this following code:

  • <group_name> is your cron group name. You can run only cron for single group at a time.
  • <job_name> is unique ID those used for identify the cron job.
  • <classpath> the identify name for itself.
  • <method> indicates a method those call in classpath.
  • <time> is schedule in cron format.

Run Cron Using Command Line

The following command allow you to run cron in Magento 2 using command line:

The –group defines the cron group to run, except the option in case of the default group.

Note: You may run cron for one group at the same time. You must run cron twice (First time for discover tasks to run and for second time run the tasks themselves)

Quality Magento Hosting

For online store owners, it is a big blessing to not get tangled with complex technical issues. Why not focus on your core strength, selling? This is why, cron jobs are an easy to manage task on the Cloudways Magento Hosting Platform. Try it by starting your free trial today. If you run into any issue, you can contact the 24/7 Live Chat Support Team who will help you with any issue.

Start Creating Web Apps on Managed Cloud Servers Now!

Easy Web App Deployment for Agencies, Developers and E-Commerce Industry

About Cloudways

Cloudways is a European MSP that provides custom cloud design, deployment and management solutions on leading cloud providers.

Stay Connected:

You Might Also Like...