
Imagine if you have to send an order confirmation email to each of your customers or generate website sitemaps manually. Seems like an impossible scenario, right? All because all these and many other tasks in your store are already handled for you by the Magento 2 cron jobs.
They schedule different activities and perform them automatically without you even noticing. However, if you want to learn more about your store performance you need to learn more about the cron tasks.
So in this comprehensive guide, we'll cover everything you need to know about Magento cron jobs. You'll discover how they work and how you can run cron jobs manually directly from the admin panel.
Post Contents [hide]
What is a Magento Cron Job?
Magento cron job is one of many tools Magento uses to schedule and run repetitive tasks (such as updating inventory, sending product alerts, or generating sitemaps) instead of triggering them manually.
Cron jobs rely on the crontab (configuration file) to execute the commands and scripts to perform certain operations within the system or the Magento 2 extensions. One crontab can contain multiple cron jobs that are related to a specific application or user.
Once you install Magento you need to install cron jobs. They are already scheduled in the cron_schedule table in the Magento database. Correspondingly, to change the schedule you need to edit the time string represented in five values.
e.g. if you want the cron job to run every 1st day of the month at 2 pm, you should put 0 2 1 * *, if you want the cron job to run every Monday, every 15 minutes put 1 5 * * 1.
Magento uses the magento.cron.log file in the var/log folder to store all cron job information, including:
- Cron job ID and code
- Status
- Created, Scheduled, Executed, and Finished dates
Thus, if some tasks in your store, like sending transactional emails, aren't executed, magento.cron.log is where you should start troubleshooting the issue. That's also when you need to know what commands to use to install, execute and check cron jobs.
Magento Cron Jobs CLI Commands
Although Magento cron jobs run by the schedule you configure, sometimes you might need to execute them manually.
Note: you need to run the commands under the Magento file owner.
For these cases, you'll need the most common commands for the Magento crons:
- To install crontab:
bin/magento cron:install
- To check crontab:
crontab -l
- To remove all Magento cron jobs of a particular user:
crontab -r
- To run the cron job:
bin/magento cron:run
- To run cron group:
bin/magento cron:run --group [cron group name]
e.g.
bin/magento cron:run --group index
bin/magento cron:run --group default
- To remove Magento crontab:
bin/magento cron:remove
How to Configure Magento 2 Cron Jobs?
Even though Magento stores information about the cron jobs in the magento.cron.log file, it isn't stored indefinitely. Magento deletes this information after a while. Thus, if you need to extend that period, you need to configure cron jobs in Magento.
1. Navigate to Stores > Configuration > Advanced > System > Cron (Scheduled Tasks). Here you'll find default cron group along with custom cron jobs from various Magento 2 extensions you use.
2. Options are the same for each cron, so you just have to fill them out with the preferred value.
3. Define how often you want cron to generate the schedule in the Generate Schedules Every field.
4. Specify the time (in minutes) for how long in advance to schedule a cron job in the Schedule Ahead for field.
e.g. if you set 30 here, cron jobs will be scheduled for the next 30 minutes after the cron runs.
5. Set the time (in minutes) which assigns the Missed status to the cron job if it doesn't run after a scheduled time in the Missed if not Run Within field.
6. Set the time (in minutes) after which the history of completed tasks should be cleared in the History Cleanup Every field.
7. Define the Success History Lifetime which defines the time (in minutes) during which the cron jobs with the Successful status should remain in the database.
8. Set how much time cron jobs with the Error status should remain in the database in the Failure History Lifetime field.
9. Choose whether you want to run all cron jobs from the group separately in Use Separate Process.
Don't forget to Save the settings and repeat it for other cron job groups if required.
Unfortunately, these are the only admin panel settings for the Magento cron job. So, you still need to execute and schedule them in the database. But not if you follow our advice.
Execute and Schedule Magento Crons in the Admin Panel
Even if you're not a technical person, you can execute cron jobs manually from the admin panel if you use the Magento 2 Cron Schedule Extension. It is really straightforward which allows you to easily manage Magento cron tasks.
To start navigate to System > Cron > Cron Jobs and start filtering through the corn jobs you'd like to reschedule or rerun.
Once you find a corn job you need, click on it to open the editor and change the schedule. If you need a tip for how cron schedule time values work, jump to the beginning of this guide.
To execute some specific cron jobs, tick them and select the Execute option from the mass actions dropdown. The selected cron jobs should be executed immediately.
Additionally, you can also disable or enable Magento cron tasks from the same grid. Again, tick all the cron jobs and select the Status > Enable/Disable from the mass actions.
As you can see, Magento cron jobs are vital for different Magento tasks unless you want to run reindex, update currency rates, and send newsletters manually.
That's why knowing how to set up and run cron jobs in Magento comes in handy for both developers and store admins. The task is even more simplified if you can check the cron job status and run cronjobs manually directly in the admin panel, don't you agree?