How to run cron jobs in Magento 2

How to run cron jobs in Magento 2

With regards to Magento 2 and cron jobs, there are a few upgrades contrasting with Magento 1.

It is still preoccupied in the way that you don’t generally run jobs straightforwardly, rather, you utilize cron syntax to setup periodical method execution by means of Magento scheduler that chips away at top of system’s cron utility.

Here are some valuable clues to help you begin with setting up your own.

Database doesn’t appear to be changed when we contrast it and Magento 1 yet settings does.Magento 2 will execute entries from cron_schedule table simply like in Magento 1.

News in setup is that now we can amass our jobs and picked in the event that we need our jobs to be executed in parallel as isolated procedures which could come exceptionally convenient at times.

magento-2-cron-configuration

In this way, with a specific end goal to run planned tasks in Magento, first we have to ensure that we arrange the accompanying jobs to run by means of system cron. This is most straightforward approach to do it:

*/1 * * * * /path/to/php /path/to/magento2/installation/bin/magento cron:run [>> /log-file &]

*/1 * * * * /path/to/php /path/to/magento2/installation/update/cron.php [>> /log-file &]

*/1 * * * * /path/to/php /path/to/magento2/installation/bin/magento setup:cron:run [>> /log-file &

As of right now you have each one of those Magento 2 default jobs prepared to go!

All in all, how to make your own job?

Pretty much, this is done in the comparable route as in Magento 1.To begin with you require crontab.xml inside your custom module:

<?xml version=“1.0”?>

<config xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=“urn:magento:module:Magento_Cron:etc/crontab.xsd”>

   <group id=“default”> <!– This Doesn’t need to be unique –>

         <job name=“cron_example” instance=“MageAddon\CronExample\Cron\Example” method=“execute”>

             <schedule>* * * * *</schedule>

         </job>

   </group>

</config>

Design above is to ensure that each minute,MageAddon\CronExample\Cron\Example:execute() is run which would (concurring the code beneath log it’s name each minute.)

 <?php namespace MageAddon\CronExample\Cron; class Example { protected $_logger; public function __construct(\Psr\Log\LoggerInterface $logger) { $this->_logger = $logger;
    }
 
    public function execute() {
        $this->_logger->info(__METHOD__);
        return $this;
    }
  }

It is additionally worth saying that you could ran stand out job bunch at the time.
Here is the means by which you do it:

 magento cron:run [group="cron_group_name"] 

Let me know your thoughts

I trust this helps you with your cron jobs setup – how might you go about this and do you have a few tips and tricks to share?

About Dat huang

Dat is a founder and Developer at Mageaddon.He is a magento lover, he enjoys listening music, playing table tennis, swimming whenever he is Free

Leave a Reply

Your email address will not be published. Required fields are marked *

*