Magento: How to run/set cron in Magento
Ever came with the requirement where you need to set cron for periodically running certain jobs automatically? If you are new to cron in Magento, this blog post is going to help you out. Cron is a time-based job scheduler in Unix-like computer operating systems.
There are basically two crons: one is system’s cron (Unix, where it will trigger the specified file, for each specified time (in terms of minute, hour, day, week, year) AND another is Magento’s internal cron where it will decide which file and method to call to complete the operation.
– First, you need to add following snipped in your module’s config.xml file, inside
<crontab> | |
<jobs> | |
<cron_name_must_be_unique> | |
<schedule> | |
<!-- This runs cron job daily at 12am mid-night --> | |
<cron_expr>0 0 * * *</cron_expr> | |
</schedule> | |
<run> | |
<!-- The below module's model file's method name is going to be called each time cron is run --> | |
<model>modulename/modelname::methodname</model> | |
</run> | |
</cron_name_must_be_unique> | |
</jobs> | |
</crontab> |
– Now open crontab of your system, by typing this in terminal:
crontab -l
This will list the cron defined in system, check if there is already cron set up for Magento, you don’t need to do anything here.
Else, type this in terminal:
crontab -e
and add the below line at the end of file:
*/5 * * * * /bin/sh /var/www/magento/cron.sh
(this will run system’s cron every 5 minutes, you can change it as per your need)
2 Comments
Leave a comment
Welcome to my Blog
Certifications
Honor
Recognition
Contributions
Categories
- Apache (2)
- ChatGPT (1)
- Domain name (2)
- eCommerce (2)
- htaccess (1)
- Humor (3)
- Instagram API (1)
- jQuery (4)
- JSON (1)
- Linux (10)
- Magento (142)
- Magento admin (58)
- Magento Certification (5)
- Magento error (13)
- Magento frontend (68)
- Magento Imagine (2)
- Magento Interview (5)
- Magento Master (2)
- Magento2 (10)
- Mobile (1)
- MySQL (7)
- OpenAI (1)
- OroCRM (2)
- Performance (2)
- PHP (8)
- Prototype JS (3)
- Security (4)
- Wordpress (3)
- XML (2)
Hi Kalpesh,
We have used your code for set up cron job by module but this is not working with my module , may be we have done something wrong, can you please help us. Below is code we have added for setup cron job.
1.0.0
Roor_Test_Block
Roor_Test_Helper
Roor_Test_Model
singleton
Roor_Test_Model_Product_Observer
addproduct
*/5 * * * *
test/etest::testCronjob
Thanks,
Jack
Thanks for sharing this post, i was looking for help in fixing cron job for weekly email setup, https://www.cloudways.com/blog/setup-magento-cron-job/, this post also helped me to configure it.