Before I tell you about the cron jobs in Magento 2.0, let me remind you how it can be enabled in Magento 1.x.
Magento 1.x
First of all, you need to create a module (I hope that if you are reading this article, you remember how it can be done :) ). After that you need to add the module to the config.xml file.
1 2 3 4 5 6 7 8 9 10 |
... < crontab > < jobs > < belvg_cron > < schedule >< cron_expr > * * * * *</ cron_expr ></ schedule > < run >< model > mymodule / cron:: myCronFunction</ model ></ run > </ belvg_cron> </ jobs > </ crontab > ... |
Next step is to create a file model:
app/code/Belvg/Mymodule/Model/Cron.php.
1 2 3 4 5 6 7 8 |
class Belvg_Mymodule_Model_Cron { public function myCronFunction() { Mage::log(‘Hello form Belvg’); return $this; } } |
This task is triggered every minute and writes messages in the log.
Now I will tell you how to enable cron jobs in Magento 2.0.
How can you do it?
This is also relatively easy and can be done in 2 steps:
1. Create a file app/code/Belvg/Mymodule/etc/crontab.xml
app/code/Belvg/Mymodule/etc/ with the following content:
1 2 3 4 5 6 7 |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd"> <group id="default"> <job name="belvg_mymodule_cron" instance="Belvg\Mymodule\Model\Cron" method="myCronFunction"> <schedule>* * * * *</schedule> </job> </group> </config> |
2. Create a class app/code/Belvg/Mymodule/Model/Cron.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
namespace Belvg\Mymodule\Model class Cron { protected $logger; public function __constructor( \Psr\Log\LoggerInterface $logger ) { $this->logger = $logger; } public function myCronFunction() { $this->logger->info(‘Hello form Belvg’); return $this; } } |
That’s it.
We created a cronjob in Magento 2. Now in logs: var/log/system.log, you can see the following message:
[2016-01-14 08:00:00] main.INFO: Hello from Belvg
Hope it will help to adapt your extensions for Magento 2.0.