Cron Job in Magento 1 and Magento 2 (Developer Certification Exam)

Sep 12, 2017340
Cron Job in Magento 1 and Magento 2 (Developer Certification Exam)

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:

Then in model/observer.php:

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:

Partner With Us Let's discuss how to grow your business. Get a Free Quote.
Talk to Andrey

And in Cron/Test.php:

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.

Perform this in etc/cron_groups.xml:

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.


Partner With Us Looking for a partner that will help you to grow your business? We are the right company to develop your webstore. Feel free to get in touch with us. We will be happy to discuss your business opportunities and provide you with a Free Quote. Talk to Andrey

Post a new comment

top
BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *