Hello to Magento blog readers!
Today we will be talking about a simple yet important tool called Cron and its usage for Magento 2 stores.
As a Magento 2 store owner, you should know the basic facts about this instrument and how to use it with third party Magento 2 extensions, as a lot of them are collaborating with Cron for performing various tasks. So here is a Magento cron job tutorial for you.
- We developed Cron Scheduler
- See How Cron Scheduler Works!
- What is cron?
- How Magento 2 uses cron
- How to start working with cron on Magento 2
- HOW TO CONFIGURE MAGENTO CRON JOB
- How To See The List Of Current Completed Or Pending Cron Tasks
- Setup Cron In The Magento 2 Admin Panel
- How To Know That Cron Is Working For Your Magento 2 Store
- How To Run Cron Manually In Magento 2
- How To Create Your Own Cron Job For Your Custom Extension
- Cron And Amasty Extensions
- Last but not least
We developed Cron Scheduler
Before you will go further, we want to tell you amazing news! Amasty team is always looking for the best solutions to make your work with Magento 2 easier. So we have developed a new mod – Cron Scheduler, which makes work with cron job much easier. In this extension, you can:
- track and manage all scheduled processes;
- correct and prevent errors in time thanks to real-time notifications;
- run Magento 2 cron jobs manually and more.
Also, there you will have a timeline, where you can see all tasks in chronological order. Moreover, in this mod, third-party modules together with native Magento ones are displaying in one grid. Try it right now!
See How Cron Scheduler Works!
What is cron?
In a nutshell, cron is a task scheduler and works on a Unix or Linux hosting environment. It lets applications automatically run specific server actions (running apps, scripts and stuff) using a predefined schedule or simply at specified time points. It can be used by Magento 2 installation and by many other web applications as well.
How Magento 2 uses cron
It is used in a list of Magento 2 features at least once. Here’s the list of them:
- Catalog price rules
- Generating Google sitemaps
- Customer Alerts/Notifications (product price change, product back in stock)
- Private sales (Magento Commerce only)
- Automatic updating of currency rates
- All Magento e-mails (including order confirmation and transactional)
Tip: you have to run cron job as a Magento 2 file system owner, not as a root user.
To go for a real example, if you’re sending back in stock notifications, Magento 2 will use cron job to trigger email dispatch according to a schedule you’ve previously set. Another example: if you have a large store with an impressive number of customers and products, you need all this data to be reindexed several times a day. Performing this task manually will take a lot of your time. Cron will automatically trigger reindexing in the background, and you won’t be worried about running this task any more. Same to the previous example, cron job in Magento 2 triggers currency rates updates, sitemaps generation, and such.
How to start working with cron on Magento 2
HOW TO CONFIGURE MAGENTO CRON JOB
Please read this article to know how to add a new Magento 2 cron job via SSH or cPanel. Also, this blog post has information on typical cron issues you might catch on if your job is not working for some reason.
How To See The List Of Current Completed Or Pending Cron Tasks
To see the list of cron tasks, go to your Magento 2 database table named cron_schedule. It looks like this in phpMyAdmin:
Here are the columns explained:
- job_code – the name of the Magento 2 cron job name
- status – current status of the task (more about the status types below)
- messages – this column is used for storing exceptions and various errors occurred while running cron jobs
- created_at – the date and time of a task creation
- scheduled_at – the scheduled date and time of a cron task execution
- executed_at – the date and time for task execution start
- finished_at – the date and time for cron task execution finish
Types of cron job statuses:
- pending – a scheduled cron job awaits to be executed (the planned date and time of execution can be found in the scheduled_at column)
- running – this status means that the cron job was started and is being executed now
- success – means that the cron job was executed without errors
- missed – this status appears when the previous execution hadn’t been finished before the new processes started. You can set the time period after which, in that case, the job will be marked as missed, with the help of the following setting in the Magento 2 admin panel:
- error – this status appears if the cron job execution wasn’t successful. If you see this, go to the messages column to check the error happened.
Setup Cron In The Magento 2 Admin Panel
To configure cron settings in the Magento 2 admin panel, go to Stores -> Configuration -> Advanced -> System -> Cron (Scheduled Tasks).
To make sure cron works with Magento 2, you have to fill in the fields.
Here’s how the settings look like:
As you see from the screenshots, in Magento 2 we have two groups for cron, called index and default.
Here are the cron jobs related to the index group:
And here are the cron jobs related to the group group:
Here are the Magento 2 admin panel cron settings explained in detail:
- Generate Schedules Every – cron will run each N minutes, N is the number to be entered here.
- Schedule Ahead for – this time will be added to the scheduled_at time
- Missed if Not Run Within – after which period of time the missed status will be appointed to the new job after the previous cron job didn’t end (see above)
- History Cleanup Every – the history will be cleared each N minutes, N is the number to be entered
- Success History Lifetime – the log of successful tasks will be cleared each N minutes, N is the number to be entered here. This option is useful if you want to control the size of your cron logs
- Failure History Lifetime – the log of unsuccessful tasks will be cleared each N minutes, N is the number to be entered here. This option is useful if you want to control the size of your cron logs too, but you may want to make this number bigger so you can access the unsuccessful logs for troubleshooting
- Use Separate Process –use this setting when you have a lot of processes to ensure you won’t get the missed ones. However, there may be further issues with the setting, and if you experience some, please consult with your system administrator
How To Know That Cron Is Working For Your Magento 2 Store
- Clear the “cron_schedule” table. You can do it by running the truncate cron_schedule; command.
- Wait a couple of minutes for the table to be cleared and run the following command: select * from cron_schedule;
- After that you can open the cron_schedule table. If everything is OK, you will see the generated data without errors there.
How To Run Cron Manually In Magento 2
Option 1: Run cron from Magento 2 CLI using the following command:
magento cron:run [–group=”<cron group name>”] , where –group – is one of the cron groups mentioned above. Skip the group mention, if you want to run cron for all the groups. Also, it’s better to run this command twice, as the first execution will schedule the cron tasks, and the second one will execute the tasks from the generated queue.
Option 2: Secure execution from browser using cron.php
Follow the instructions from the official Magento docs to use this option.
How To Create Your Own Cron Job For Your Custom Extension
I’ll show you how to do this using an example of Amasty Follow Up extension.
Create a crontab.xml, place it into /app/code/Amasty/Followup/etc/crontab.xml. It should have the following contents:
- group id is the group where the cron job will be executed
- Fill job name with a name you’re giving to your cron job. Later it will be shown in the job_code column in the tasks table
- instance is the class which will be called for the cron job execution
- method is the name of the method of the class mentioned above, which will be called for the cron job execution
- <schedule></schedule> is a place to set up the schedule
Note: you can mention several cron jobs here. Set up the schedule using the Unix standard and employ this tool for setting up the schedule for your convenience.
Now, create a class in the following path:
All the classes responsible for cron execution should be stores here:
And here’s the contents of the Amasty\Followup\Cron\RefreshHistory class:
The execute method, naturally, executes the actions we want to be run by cron.
Cron And Amasty Extensions
And of course we are using cron in a number of Amasty extensions, such as:
- Follow Up Email
- Abandoned Cart Email
- Smart Review Reminder
- Reward Points
- Product Feed
- Google Sitemap
For example, cron is employed for generating queues of abandoned cart emails dispatch, automatic coupons generation, assigning rewards points, sending gifts for customers’ birthdays, sitemaps and feeds generation, and so on.
All this cron job you can view conveniently in chronological order in Cron Scheduler mod on a handy grid. It works perfectly with all extensions for Magento 2. You can try it right now!
Last but not least
If you’re installing any third party extensions which use cron on Magento 2 and experiencing typical troubles (such as – emails not sending, feeds not generating), the first thing to do is to check if your Magento 2 cron is set up correctly.
As always, if you’re experiencing troubles and can’t solve them anyway, we always recommend to ask your system administrator to help you manage the issue.
That’s a wrap for today. We hope that this article gave you a general understanding of cron as a tool and how to use it on Magento 2. Still have any questions? Leave them in the comments below, and we’ll do our best to help you.