The cron job is a free operating system with amazing features developed by Linux. It will create a script or command which is appropriate to the things you want to do. Apart from manual working, this particular job allows running automatically at an exact date and time. Because of its automation, Cron job is the best choice for the repetition of the projects. This configuration is very useful in the Magento framework, as it is used to schedule many system activities such as auto-update of currency rates, Magento emails, reindexing, etc. After all these operations, when the configuration is correct, then only the Cron job is active. This Cron job uses a default file named as Cron Log. That is used to change the setup of this job. The term used here, known as cPanel, is a web-based hosting control panel offered by the provider to the owners. It helps to manage the websites from a web-based interface.This Cron Job Cpanel has many features like web disk, disk usage, FTP Accounts, Dictionary Privacy, Anonymous FTP, etc. Basically, these Cron jobs can execute internal Magento Cron job and controls which Cron jobs are executed and when.
Magento 2 includes multiple Cron jobs to start various tasks such as applying price rules, reindexing, generating sitemaps, and many other critical activities at a given time. There are many amazing features available for Magento 2 which requires at least one Cron job. These Cron job helps in scheduling activities to happen in the future. The activities are listed below;
- Generating Google Sitemaps
- Catalog price rules
- All Magento emails
Some important tasks used before the configuration of cron in Magento 2;
Being a master user, log in to the Magento 2 cron. Master user has permission to write to the Magento 2 file system. If you are using the bash shell, then you can use the following syntax to switch to the Magento file system owner;
su <Magento 2 file system owner> -s /bin/bash -c <command>
To run the commands of <Magento 2 from any directory>, add <Your Magento 2 install dir>/bin to your system path. The bash shell examples are as follows;
There are many other ways which help in running commands;
If you have
cd <your Magento install dir>/bin
Then, you can run this command as
. /magento <your command name>
And the alternative way is
php <your Magento 2 install dir>/bin/magento <command name>
Where, the subdirectory is <your Magento 2 install dir>, which is a web server.
These are such steps required to Create Cron Job Programmatically. And the steps are described below;
Create The Magento Crontab
Let’s start with version 2.2 where a Crontab is created by Magento. Here, you have to add the Magento crontab to any configured crontab for the Magento file system. On the other hand, after configuring the crontabs for other extensions, then only you will be able to add the Magento crontab to it.
To create a Magento crontab following steps are followed;
- First of all, you have to switch on or log in to the Magento file system owner.
- After this, you have to change your Magento installation directory.
Finally, enter the following commands for the installation,
Php bin/Magento cron:install [--force]
- This “–force” is used for rewriting an existing Magento crontab.
To view the designed crontab again, you have to type the following commands;
#~ MAGENTO START
* * * * * /usr/bin/php /var/www/html/magento2/bin/magento cron:run | grep -v Ran jobs by schedule >> /var/www/html/magento2/var/log/magento.cron.log
* * * * * /usr/bin/php /var/www/html/magento2/update/cron.php >> /var/www/html/magento2/var/log/update.cron.log
* * * * * /usr/bin/php /var/www/html/magento2/bin/magento setup:cron:run >> /var/www/html/magento2/var/log/setup.cron.log
#~ MAGENTO END
Then we have to go to the next step of the configuration process. How to Setup Cron Job in Magento 2 Admin:
Here to create the setup, you have to follow the points given below;
- First of all, go to the Magento Admin page to click Stores.
- Then go to the setting section and select the option of Configuration.
- Here, in Cron Configuration Admin, choose the system under “Advanced” section, present in the panel located on the left side.
- Then you will have to open the Cron section and continue the following;
- Then open the cron configuration option for a group: Index and cron configuration options for a group: Default sections.
At last, complete the following information. There are many options for the group index. Those are given below;
- Fill the information in the section "Generate Schedule Every".
- Then give the data for the option “Schedule Ahead for”.
- If anything is missing, please mention in which it runs. And the option given for this is “Missed if not Run Within”.
- Make sure to fill the information for the given option. i.e. “History Cleanup Every”.
- Give all the data for “Success History Lifetime”.
- And also fill up for “Failure History Lifetime”.
- At last, fill up for the option “Use Separate Process”.
Run Cron From The Command Line
Some of the command options to run the crontab are as follows
bin/magento cron:run [--group="<cron group name>"]
To Run Specific Cron job which is considered as indexing jobs, you have to enter the command that is written below;
bin/magento cron:run --group index
For running the default cron job, you will have to enter the following command;
bin/magento cron:run --group default
To Run The Specific Cron Job Manually, see configure cron jobs as well as cron group.
A Magento module is considered as a logical group. That is directory helpers, controllers, containing blocks, etc. This is related to a specific business feature. This concept contains one feature and has a minimum number of dependencies on other modules. This Magento 2 framework provides a set of core logic such as; libraries, PHP code, and all other basic functions which are produced by the modules.