Running cron jobs in Magento 2

When it comes to Magento 2 and cron jobs, there are some improvements comparing to Magento 1.
It is still abstract in the way that you don’t really run jobs directly, instead, you use cron syntax to setup periodical method execution via Magento scheduler that works on top of system’s cron utility.
Here are some useful hints to help you get started with setting up your own.
Database doesn’t seem to be changed when we compare it with Magento 1 but configuration does.
Magento 2 will execute entries from cron_schedule table just like in Magento 1.
News in configuration is that now we are able to group our jobs and chose if we want our jobs to be executed in parallel as separate processes which could come very handy sometimes.
So, in order to run scheduled tasks in Magento, first we need to make sure that we configure the following jobs to run via system cron. This is most simple way to do it:
*/1 * * * * /path/to/php /path/to/m2/installation/bin/magento cron:run [>> /log/file &]
*/1 * * * * /path/to/php /path/to/m2/installation/update/cron.php [>> /log/file &]
*/1 * * * * /path/to/php /path/to/m2/installation/bin/magento setup:cron:run [>> /log/file &
At this point you have all those M2 default jobs ready to go!
So, how to make your own job?
More or less, this is done in the similar way as in Magento 1.
First you need 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"> <!-- Doesn't need to be unique -->
<job name="cron_example" instance="Inchoo\CronExample\Cron\Example" method="execute">
<schedule>* * * * *</schedule>
</job>
</group>
</config>
Configuration above is to make sure that every minute, Inchoo\CronExample\Cron\Example:execute() is run which would (according the code below log it’s name every minute.)
<?php
namespace Inchoo\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 also worth mentioning that you could chose to run only one job group at the time.
Here is how you do it:
magento cron:run [--group="cron_group_name"]
Your thoughts appreciated
I hope this helps you with your cron jobs setup – how would you go about this and do you have some tips and tricks to share?
We would be glad to offer you some extra help, we can offer you a detailed custom report based on our technical audit – feel free to get in touch and see what we can do for you!
10 comments
Should i need to create a custom module to perform ” reindexing ” every hour via cron ?
What is use of
magento setup:cron:run
Its eating too much cpu
My server team saying I also need to add magento/update/dev/shell/cron.sh in my server to run cron jobs successfully. But I can’t see any such recommendation in devdocs documentation. Also It says cron.sh will not work in magento2. http://i.prntscr.com/PdaL9fGqRBSjskEiNxjg3g.png
What is the purpose of this cron.sh file? Do we have to add this file after adding following in crontab?
* * * * * /bin/magento cron:run | grep -v “Ran jobs by schedule” >> /var/log/magento.cron.log
* * * * * /update/cron.php >> /var/log/update.cron.log
* * * * * /bin/magento setup:cron:run >> /var/log/setup.cron.log
‘/1’ seems unnecessary to me…?
Hi,
I did exactly as is said in the article above. When I ran
$ magento cron:run
, I could see the execute entry in var/log/system.log file. But that’s it. The cron job did not auto run after that. In crobtab.xml it is set to run every minute. And In group:default configuration, it is set to run every 15 minutes. But it has not run in either case. What could be going wrong ?Thanks,
Azher
Hi,
I meet this issue “update-cron.ERROR: Cron readiness check failure! Found following non-writable paths [] []” when I try to run cron in my O.S Ubuntu hosted in AWS Amazon. I gave permission access but nothing change. Please can you help me to find a solution?
Thanks for sharing an useful post like this. It will helps to all Magento 2 Developers!!.
Thanks Tomas Novoselic..Thanks Inchoo!!.
If you do setup your own cron group (e.g., you use something other than “default”) then you will need to create a cron_group.xml file in your extension. Search the Magento codebase for other “cron_group.xml” files to determine the pattern.
Just for information: If you want to run cron job directly without having to wait for the schedule (e.g when testing), you can use
(same way as Magento 1)
Wow! I didn’t know that magerun is ready for the Magento 2, thank you for info 🙂