How to Configure Magento Cron Job
Magento offers the facility to schedule repetitive tasks automatically such as to send emails, newsletters, currency rate updates, log cleanups, google site map generation, and more.
As many merchants are still using Magento 1, the solution may be helpful to them to save themselves from such tasks.
Commands for Magento Cron:
Magento 1.x
1 |
[php]sh /path/to/your/magento/site/root/cron.sh [/php] |
Magento 2.x
Refer How to Set Cron in Magento 2
Also, note that you need to change the /path/to/magento/root/folder to the path of your website’s default folder.
You have the option to run cron via your store URL in Magento 1.x, however, it is not recommended. Doing so causes issues with running multiple cron process simultaneously resulting in performance troubles or task may not be completed successfully.
Method to Add a New Cron Task via SSH
- Log in to the server via SSH
- Run crontab -e command to add a cron task
- You can add or edit the cron task in the text editor
- Add the following record:
1[php]*/5 * * * * sh /path/to/magento/root/folder/cron.sh[/php] - Save
- crontab -l command will show the newly created task.
Method to Add a New Cron Task via Cpanel
- Log in to your site’s configuration panel at http://yourmagentosite.com:2082/ or https://yourmagentosite.com:2083/.
- Enter cron into the search field to see the Cron jobs option in the Advanced section.
- Enter the Email address in the Cron Email section which is used to send cron messages in case of any issues.
- Schedule the cron job and the command to be run.
- Click “Add New Cron Job” button to complete the configuration.
Now that you are done with the cron job configuration, you can check the cron_schedule table of the Magento database to see the changes. The changing content is the indication of configuring the Magento cron correctly. Dynamically Schedule Cron Job in Magento 2 System Configuration if you require to backup your whole store based on the hours.
Do not attempt to manually change the database table.
Frequency to set the Magento Cron Job:
It is a good practice to run a cron job every 5 minutes for a mid-size Magento store. It may vary as per the requirement from one hour to one minute, however, 5 minutes is my recommendation.
When you set up Magento cron job for 5 min via SSH, the command is:
1 |
*/5 * * * * /usr/bin/php /path/to/magento/root/folder/bin/magento cron:run | grep -v "Ran jobs by schedule" >> /path/to/magento/root/folder/var/log/magento.cron.log |
1 2 |
*/5 * * * * /usr/bin/php /path/to/magento/root/folder/update/cron.php >> /path/to/magento/root/folder/var/log/update.cron.log |
1 |
*/5 * * * * /usr/bin/php /path/to/magento/root/folder/bin/magento setup:cron:run >> /path/to/magento/root/folder/var/log/setup.cron.log |
That was all you need to know about the Magento cron job configuration.
However, while doing so, there are certain things you need to take care of such as permissions for /path/to/magento/root/folder/cron.sh file. The permission set for the file should allow the execution of the command.
Additionally, check if the correct Email address is used in MAILTO. It is necessary because any issues with the cron job and the help for it is sent to this Email ID.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
How to Call Helper Function in phtml in Magento 2
How to Add Category Attribute to Custom Attribute Group in Magento 2
Next