Magento cron job is one of many tools Magento uses to schedule and run tasks instead of triggering them manually. Cron jobs execute the script you set in the configuration to run certain operations within the system as well as the operations in Magento 2 extensions

After you create cron job in Magento you have to configure the cron options to make it run accordingly. Here you'll learn how to configure cron in Magento 2 admin and how to manage it with several useful commands.

To Configure Cron in Magento 2:

1. Navigate to Stores > Configuration > Advanced > System > Cron (Scheduled Tasks).

You will see all of the default cron jobs under the Cron (Scheduled Tasks) tab along with the cron jobs that go with various Magento 2 modules you use. Options are the same for each cron, so you just have to fill them out with the preferred value.

Magento 2 Cron Scheduled Tasks

2. Configure cron schedule options.

3. Define how often you want cron to generate the schedule in Generate Schedules Every.

4. In the Schedule Ahead for field specify the time (in minutes) for how far in advance to schedule the cron job.

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. In the History Cleanup Every field, you set the time (in minutes) after which the history of completed tasks should be cleared.

7. Define the Success History Lifetime which specifies the time (in minutes) during which the cron jobs with the Successful status should remain in the database.

8. Set how much time should the 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.

Configure Cron Job in Magento 2

Don't forget to Save Config and repeat it for other cron jobs if required.

Useful Commands to Work with Cron Jobs in Magento

Though crons in Magento run by schedule you configure them to, sometimes you might need to execute them on demand, manually. 

Note: you need to run the commands under the user who is the owner of Magento.

For those cases you need the most common commanda to use with Magento crons:

  • To create crontab:
bin/magento cron:install 
  • To check crontab:
crontab -l
  • To remove all Magento crons of this user:
crontab -r 
  • To run cron job:
bin/magento cron:run
  • To run cron group:
bin/magento cron:run --group [cron group name]

For example:

bin/magento cron:run --group index

bin/magento cron:run --group default
  • To remove Magento crontab:
bin/magento cron:remove

Magento cron jobs are vital for repetitive Magento tasks unless you want to do reindex, update currency rates, send newsletters and customer alerts manually regularly.

That's why knowing how to set up and run a cron job in Magento comes in handy for both developers and store admins. We hope this guide will help you to manage cron jobs better.