How to Configure and Manage Cron Jobs in Magento 2?

Imagine if you have to update currency rates or catalog price rules and 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. 

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.

Install Magento Cron Job

Once you install Magento you need to add cron jobs to the cron tab of your operational system.

Note: we don't recommend doing that on you stage or dev instances, since it could lead to sending redundant newsletters to your customers.  

To register the Magento cron job in your operational system (OS) crontab, run the following command:

bin/magento cron:install

It will add a new record  in OS crontab, which you can check by running:

crontab -l

For example, you will see these new rows in the system crontab:

#~ MAGENTO START d9b3d5d01f3432040ee22c71411078a3391f3096bf5aa3e7fcc82283245f0284
* * * * * /usr/bin/php /path-to-magento-folder/bin/magento cron:run 2>&1 | grep -v "Ran jobs by schedule" >> /path-to-magento-folder/var/log/magento.cron.log
#~ MAGENTO END d9b3d5d01f3432040ee22c71411078a3391f3096bf5aa3e7fcc82283245f0284

Schedule Magento Cron

By default, Magento adds the new record with the * * * * *  value. This time string tells the system to execute Magento cron job task every minute. 

cron schedule

e.g. if you want the cron job to run every 5 minutes, you should put */5   *  *  *  *, you can read more about cron schedule configuration here.

We don't recommend changing the default value since it could lead to incorrect cron scheduling, running, or even missed Magento cron tasks. However, if you still need to change the time string, run the following command and edit it:

crontab -r

The Magento console command bin/magento cron:run schedules the Magento cron tasks and runs them according to the schedule.

Delete Cron Task

If you need to remove the Magento cron from the operational system crontab, run the following command:

bin/magento cron:remove

Debug Cron Job

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 updating cart price rules, aren't executed, check magento.cron.log or the cron_schedule table in the Magento database. The latter contains information about the errors and the error messages.

How to Configure Magento 2 Cron Jobs?

Even though Magento stores information about the cron jobs in the cron_schedule table, 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 groups from various Magento 2 extensions you use.

Magento 2 Cron Scheduled Tasks

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.

If you set it to Yes, when the bin/magento cron:run command is executed, it runs the cron group asynchronously and triggers the following command automatically:

bin/magento cron:run --group [cron group name]

Configure Cron Job in Magento 2

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 make code changes to reschedule or disable specific cron tasks. But not if you follow the instruction below.

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.

filter cron jobs in magento 2

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. 

schedule cron jobs in magento 2

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.

execute cron jobs manually magento

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. 

disable magento 2 cron jobs

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 set up cron job notifications directly in the admin panel, don't you agree?