Magento 2 comes with many awesome features. Magento 2 Cron job is one of the most stunning features. Cron helps Magento 2 store owners to setup commands which will run systematically at a set time and date. In Magento 2, Cron job runs scheduled tasks, reindexing, generating emails, generating newsletters, sitemaps and more. Basically, the cron executes internal Magento cronjob manager and manages which cron jobs are executed and when.
Cron is a vital part of Magento. Magento 2 Cron job configuration is highly important because many Magento features are based on scheduled activities, possible only via cron jobs. Here’s a list of some of them:
- Private sales (Magento Enterprise Edition)
- Automatic update of currency rates
- Magento emails
- Customers alerts and notifications
- Generating Google sitemaps
- Catalog price rules
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.
Note: You will need Magento server level access to run cron jobs properly. Running cron jobs on a shared server can lead to performance issues that can affect your account.
Your Managed Hosting Solution that Offers Cron Job Scheduling
Automate repetitive tasks with Cron Job scheduling and improve your store experience.
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:
su <Magento 2 file system owner> -s /bin/bash -c <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
cd <your Magento install dir>/bin
You can run this command as
./magento <your command name>
php <your Magento 2 install dir>/bin/magento <command name>
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.
After web server configuration, PHP command-line configuration is required. Use these commands for command-line configuration:
php -i | grep php.ini
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 Magento 2 cron job, you must login as a user with root privileges (as I mentioned before) and run following command:
crontab -u <Magento 2 file system owner user name> -e
“For example” crontab -u magento_cloudways -e
Use the following commands, but you may need to choose a text editor first.
*/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
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.
*/1 * * * * /usr/bin/php -c /etc/php5/apache2/php.ini /var/www/magento2/bin/magento cron:run > /var/www/magento2/var/log/magento.cron.log& */1 * * * * /usr/bin/php -c /etc/php5/apache2/php.ini /var/www/magento2/update/cron.php > /var/www/magento2/var/log/update.cron.log& */1 * * * * /usr/bin/php -c /etc/php5/apache2/php.ini /var/www/magento2/bin/magento setup:cron:run > /var/www/magento2/var/log/setup.cron.log&
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:
<config> <group id="<group_name>"> <job name="<job_name>" instance="<classpath>" method="<method>"> <schedule><time></schedule> </job> </group> </config>
- <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:
magento cron:run [--group="<cron group name>"]
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 Magento 2 cron job is an easy to manage task on 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.
Boost Your Magento Store Performance by 5x Times & Maximize Your Sales
Our fastest Magento hosting can help you in growing your business revenue by 500%
Fayyaz, a passionate Motorbike tourist, works as a Team Lead — Magento Community at Cloudways - A Managed Magento Hosting Platform. His objective is to learn & share about PHP & Magento Development in Community. You can contact him at email@example.com