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).

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, consider 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

Add cron jobs to .magento.app.yaml

You should add all cron jobs to your .magento.app.yaml file in the the crons section.

The default minimum frequency for cron jobs is 5 minutes in Starter and Pro Integration environments and 1 minute in Pro Staging and Production environments, but you can change this setting in .magento-app-yaml.

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.

We manage cron jobs on Pro plan Staging and Production environments using Jenkins. These cron jobs do not run precisely against the system clock. If Jenkins encounters lag, the cron jobs may run occassionally late.

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 to review, remove, or modify these cron jobs. To update a cron job, we recommend pushing the updates through to the Integration master branch in the .magento.app.yaml file. Cron jobs for Pro plan Staging and Production environments are not available through a Cron tab.

Troubleshooting

Having trouble with cron jobs? The information in this section may help.

Reset cron jobs

Sometimes, Magento cron jobs just don’t finish executing and get stuck, which prevents other Magento cron jobs from running. These jobs can become stuck and require unlocking to allow additional crons to continue and to free resources.

Symptom

Magento cron jobs may become stuck causing other cron jobs to fail to execute on schedule, which causes adverse impacts on your store. These crons become stuck for a number of reasons, including network issues, application crashes, redeployment issues, and so on. To resolve, you need to unlock the stuck cron job.

Symptoms of stuck cron jobs include:

  • Large quantity of jobs appearing in the cron_schedule queue
  • Degraded performance
  • Some jobs fail to execute on schedule
  • Jobs aren’t executing according to schedule

Solution

  • This solution resets all cron jobs, including those currently running, so we recommend using it only in exceptional cases. For example, when you’ve verified that cron jobs are stuck and causing issues. Re-deployment runs this command by default to reset cron jobs, so they appropriately recover after the environment is back up.

  • Avoid using this solution when indexers are running.

To unlock a stuck cron job, you must manually SSH into the affected environment and isue a CLI command. This command changes the status of the cron job in the database, ending the cron forcefully to allow other scheduled crons to continue.

  1. Open a terminal application.
  2. SSH into the environment experiencing the issue.
  3. Enter the following command to reset Magento cron jobs:

    ./vendor/bin/ece-tools cron:unlock