Magento Cron, In today’s Topic we will cover about Magento cron and how we can setup to run automatic.
Magento Cron is one type of scheduled job which runs on a particuler defined time interval
Cron Job can run every seconds , every minutes , every hours or so.
In Magento many features requires atleast one cron job which runs on a particular scheduled time interval to fullfill the requirement.
Some of the Magento activities are as below which are working based on crons
- Indexer
- Magento All Emails
- Magento Mysql message queues
- Google Sitemaps
- Newsletters and many more
You can also create your own custom cron job for your module.
First you need to create your custom module
Now create crontab.xml in etc folder and add below code
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
<group id="default">
<job instance="CreatesWowTech\ModuleName\Cron\CustomCron" method="execute" name="createswowtech_customcron">
<schedule>*/5 * * * *</schedule>
</job>
</group>
</config>
Here group id = “default” is a default group for all crons.
instance : Here you need to define your class path
method : here you need to define your method name which will be call once cron will run
name : this will be your cron unique name
schedule : Here you need to define your cron expression , you can generate cron expression as per your requirement from here
Now you need to create class file CustomCron.php under CreatesWowTech\ModuleName\Cron Folder and add below code
<?php
namespace CreatesWowTech\ModuleName\Cron;
class CustomCron
{
public function execute()
{
// Your Business Logic
}
}//end class
Cron Group
Cron group helps to process multiple cron at a time. Most magento crons use default group.
To define new cron group create crongroup.xml and add below code
<config>
<group id="custom_group_name">
<schedule_generate_every>1</schedule_generate_every>
<schedule_ahead_for>4</schedule_ahead_for>
<schedule_lifetime>2</schedule_lifetime>
<history_cleanup_every>10</history_cleanup_every>
<history_success_lifetime>60</history_success_lifetime>
<history_failure_lifetime>600</history_failure_lifetime>
<use_separate_process>1</use_separate_process>
</group>
</config>
So your crontab.xml file look like as below
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
<group id="custom_group_name">
<job instance="CreatesWowTech\ModuleName\Cron\CustomCron" method="execute" name="createswowtech_customcron">
<schedule>*/5 * * * *</schedule>
</job>
</group>
</config>
How to run Cron Job Manually
Run command php bin/magento cron:run
How to Run cron job based on cron group
php bin/magento cron:run –group=”group_name”
How to run particular cron job manualy
By Default Magento is not allowing to run particular cron job. But you can follow below steps to run cron job manually.
Go to your magento root directory and run Below command to download n98 magerun2
wget https://files.magerun.net/n98-magerun2.phar
Run Below command to make this executable chmod +x ./n98-magerun2.phar
Now to run your any cron job run below command from your magento root directory
$ php n98-magerun2.phar sys:cron:run cron_name
Here cron name is your cron name which you define in your crontab.xml file
How you can setup cron job automated.
Now we will see how you can setup your cron job automated.
First of all login to your server and go to your magento root directory
switch to your file system owner using chown
Now you can setup cron in 2 different ways
Option 1 : run php bin/magento cron:install
Please see screenshot below
Option 2
Run crontab -e
add below code
#~ MAGENTO START f73db5f5dcf223372d4c9fab56bef0c1730eece898fad5cbee0c313b46b0dd1f
* * * * * /usr/bin/php7.3 /var/www/html/magento2/bin/magento cron:run 2>&1 | grep -v "Ran jobs by schedule" >> /var/www/html/magento2/var/log/magento.cron.log
* * * * * /usr/bin/php7.3 /var/www/html/magento2/update/cron.php >> /var/www/html/magento2/var/log/update.cron.log
* * * * * /usr/bin/php7.3 /var/www/html/magento2/bin/magento setup:cron:run >> /var/www/html/magento2/var/log/setup.cron.log
#~ MAGENTO END f73db5f5dcf223372d4c9fab56bef0c1730eece898fad5cbee0c313b46b0dd1f
here please use your php version
to check php path use below command
whereis php