Cron job is the main way to run repeated tasks. It’s used in Magento for such tasks as indexing, email processing, currency rate update.
Magento 1
Setting up cron job
To configure Magento 1 cron job, you need to log into your server via SSH, run crontab -e and add the following line:
*/5 * * * * sh /path/to/your/magento/site/root/cron.sh
Save changes and close the file. This will allow cron.sh to run every five minutes.
Creating a custom cron job
In your module etc/config.xml:
1 2 3 4 5 6 7 8 9 10 |
<crontab> <jobs> <cron_job_name> <schedule><cron_expr>0 1 * * *</cron_expr></schedule> <run> <model>module/observer::execute</model> </run> </cron_job_name> </jobs> </crontab> |
Then in model/observer.php:
1 2 3 4 5 6 7 |
class Vendor_Module_Model_Observer { public function execute() { //your custom code } } |
Magento 2
To set up Magento 2 cron job properly, you need to log into your Magento server via SSH as a filesystem owner and add the following lines to crontab (crontab -e):
* * * * * /usr/bin/php /var/www/magento2/bin/magento cron:run | grep -v “Ran jobs by schedule” >> /var/www/magento2/var/log/magento.cron.log
* * * * * /usr/bin/php /var/www/magento2/update/cron.php >> /var/www/magento2/var/log/update.cron.log
* * * * * /usr/bin/php /var/www/magento2/bin/magento setup:cron:run >> /var/www/magento2/var/log/setup.cron.log
Where /usr/bin/php is a path to php binary and /var/www/magento2 is a path to your Magento 2 installation.
Creating a custom cron job
In your module etc/crontab.xml:
1 2 3 4 5 6 7 8 |
<?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"> <job name="custom_cronjob" instance="Vendor\Module\Cron\Test" method="execute"> <schedule>* * * * *</schedule> </job> </group> </config> |
And in Cron/Test.php:
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php namespace Vendor\Module\Cron; use \Psr\Log\LoggerInterface; class Test { protected $logger; public function __construct(LoggerInterface $logger) { $this->logger = $logger; } public function execute() { //custom code } } |
You can optionally set up a custom cron group in case when you need your cron job to run on a different schedule than other cron jobs or with different settings.
Partner With Us
Let's discuss how to grow your business. Get a Free Quote.Perform this in etc/cron_groups.xml:
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/cron_groups.xsd"> <group id="custom_crongroup"> <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> </group> </config> |
Where:
schedule_generate_every – frequency that schedules are written to the database.
schedule_ahead_for – time in advance that schedules are written to the database. schedule_lifetime – schedule lifetime in minutes.
history_cleanup_every – time that cron history is kept in the database.
history_success_lifetime – time the record of successfully completed cron jobs are kept in the database.
history_failure_lifetime – time the record of failed cron jobs are kept in the database.
use_separate_process – allows each cron job to run in a separate process.
Magento Extensions
Take your online store to the next level with BelVG Magento Extensions
Visit the store