Set up cron jobs

Magento uses cron jobs for numerous features to schedule activities. You can add cron jobs to .magento.app.yaml and push it to your branches for deployment. For specific information for configuring and setting up cron in Magento, see Magento Commerce cron information. The following information is specific to creating and deploying cron jobs in Magento Commerce (Cloud).

We recommend you run cron jobs as the Magento file system owner. Do not run cron as root or as the web server user.

Magento Commerce cron information

The following links provide more information on crons for Magento Commerce. You can use this information for setting up and understanding cron jobs in Magento. When you want to add cron jobs for Magento Commerce (Cloud), you manage all crons through .magento.app.yaml. For more information, review this topic.

Build a cron job

A cron job includes the specification for scheduling and timing and the command to run at that time. For example, the general format is:

* * * * * <command>

You will add the cron job to .magento.app.yaml in the crons section. The general format is spec for scheduling and cmd for the script. For example:

crons:
    productcatalog:
        spec: '20 */3 * * *'
        cmd: 'bin/magento indexer:reindex catalog_product_category'

The following example is the default cron included for Magento Commerce (Cloud).

# Default Magento 2 cron jobs
crons:
    cronrun:
        spec: "*/5 * * * *"
        cmd: "php bin/magento cron:run && php bin/magento cron:run"

We use only this one cron for cloud due to the read-only nature of the environments. This is different from Magento Commerce which has three default cron jobs.

Magento uses a five value specification for a cron job. The numbers per each * * * * * is as follows:

  • Minute (0-59) For all Start environments and Pro Integration environments, the minimum frequency supported for crons is five minutes. You may need to configure settings in your Magento Admin.
  • Hour (0-23)
  • Day of month (1 - 31)
  • Month (1 - 12)
  • Day of week (0 - 6) (Sunday to Saturday; 7 is also Sunday on some systems)

For example:

  • 00 */3 * * * runs every 3 hours at the first minute (12:00 am, 3:00 am, 6:00 am, and so on)
  • 20 */3 * * * runs every 3 hours at minute 20 (12:20 am, 3:20 am, 6:20 am, and so on)
  • 00 00 * * * runs once a day at midnight
  • 00 * * * 1 runs once a week on Monday at midnight

When determining the scheduling of your cron jobs based on the time it takes to complete the task. For example, if you run a job every three hours and the task takes 40 minutes to complete, you may want to change the scheduled timing.

For the command script, the format includes:

<path to php binary> <magento install dir>/<script with command>

The following is an example cron job:

crons:
    spec: "00 */3 * * *"
    cmd: "/usr/bin/php /app/abc123edf890/bin/magento indexer:reindex catalog_category_product"

With the settings:

  • <path to php binary> is /usr/bin/php
  • /app/abc123edf890 is the install directory, which includes the Project ID for this example
  • bin/magento indexer:reindex catalog_category_product is the script actions

Configure cron settings in the Magento Admin

Due to the minimum allowed frequency of five minutes for Starter environments and Pro Integration environments, you need to change the cron settings defaulted at two minutes in the Magento Admin. If you don’t change the settings, crons will never run.

You do not need to set this for Pro Staging and Production environments.

To configure:

  1. Log into the Magento Admin in your deployed environment: all Starter environments and Pro Integration environments.
  2. Navigate to Stores > Configuration > Advanced > System. Expand the Cron (Scheduled Tasks) section.
  3. Expand the Cron configuration options for group: index section.
  4. For the Missed if Not Run Within setting, deselect the checkbox for Use system value. Change the value from 2 to 10 minutes.
  5. Expand the Cron configuration options for group: staging section.
  6. Change the Missed if Not Run Within setting, deselect the checkbox for Use system value. Change the value from 2 to 10 minutes.
  7. Expand the Cron configuration options for group: catalog_event section.
  8. Change the Missed if Not Run Within setting, deselect the checkbox for Use system value. Change the value from 2 to 10 minutes.
  9. Save the changes.

Add cron jobs to .magento.app.yaml

You should add all cron jobs to your .magento.app.yaml file in the the crons section. We include a default cron job for Magento in the default file:

# Default Magento 2 cron jobs
crons:
    cronrun:
        spec: "*/5 * * * *"
        cmd: "php bin/magento cron:run && php bin/magento cron:run"
  1. Edit .magento.app.yaml in the root directory of the Magento code in the Git branch.
  2. Locate the crons section in the file and add your custom cron code.

    For example, you could add a reindexer cron job to run every three hours, 20 minutes after the hour (such as 12:20 am, 3:20 am, and so on):

     crons:
         magento:
             spec: '*/5 * * * *'
             cmd: 'php bin/magento cron:run && php bin/magento cron:run'
         productcatalog:
             spec: '20 */3 * * *'
             cmd: 'bin/magento indexer:reindex catalog_product_category'
    
  3. Save the file and push updates to the Git branch.

Add cron jobs to environments

When you push the code, the cron jobs will be added to and run in the following environments:

  • Starter: All environments you push to including Master
  • Pro: Only Integration environments you push to including Master

To add the cron jobs to Pro plan Staging and Production, you must enter a ticket to Support. Request to have the cron jobs in .magento.app.yaml added to those environments. We recommend pushing the updates through to the Integration master branch.

Update cron jobs

If you need to change or update your cron jobs, update the crons section in your .magento.app.yaml file. Push the file to your Git branch and deploy across environments.

For Pro plan Staging and Production environments, please enter a ticket to Support advising you need the cron jobs updated from the latest updated file. We recommend pushing the updates through to the Integration master branch.