> ## Documentation Index
> Fetch the complete documentation index at: https://developer.upsun.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Crons

> A cron dictionary that defines scheduled tasks for the app.

Optional in [single-runtime](https://developer.upsun.com/docs/configure-apps/app-reference/single-runtime-image.md#primary-application-properties) and [composable](https://developer.upsun.com/docs/configure-apps/app-reference/composable-image.md#primary-application-properties) images.

The keys of the `crons` definition are the names of the cron jobs.
The names must be unique.

If an application defines both a `web` instance and `worker` instances, cron jobs run only on the `web` instance.

See how to [get cron logs](https://developer.upsun.com/docs/observability/logs/access-logs.md#container-logs).

The following table shows the properties for each job:

| Name | Type | Required | Description |
| - | - | - | - |
| `spec` | `string` | Yes | The [cron specification](https://en.wikipedia.org/wiki/Cron#Cron_expression). To prevent competition for resources that might hurt performance, use `H` in definitions to indicate an unspecified but invariant time. For example, instead of using `0 * * * *` to indicate the cron job runs at the start of every hour, you can use `H * * * *` to indicate it runs every hour, but not necessarily at the start. This prevents multiple cron jobs from trying to start at the same time. |
| `commands` | A [cron commands dictionary](#cron-commands) | Yes | A definition of what commands to run when starting and stopping the cron job. |
| `shutdown_timeout` | `integer` | No | When a cron is canceled, this represents the number of seconds after which a `SIGKILL` signal is sent to the process to force terminate it. The default is `10` seconds. |
| `timeout` | `integer` | No | The maximum amount of time a cron can run before it's terminated. Defaults to the maximum allowed value of `86400` seconds (24 hours). |

Note that you can [cancel pending or running crons](https://developer.upsun.com/docs/environments/cancel-activity.md).

### Cron commands

| Name | Type | Required | Description |
| - | - | - | - |
| `start` | `string` | Yes | The command that's run. It's run in [Dash](https://en.wikipedia.org/wiki/Almquist_shell). |
| `stop` | `string` | No | The command that's issued to give the cron command a chance to shutdown gracefully, such as to finish an active item in a list of tasks. Issued when a cron task is interrupted by a user through the CLI or Console. If not specified, a `SIGTERM` signal is sent to the process. |

> [tab] Single-runtime image
>
> &#x20;       applications:
> &#x20;         \<APP\_NAME>:
> &#x20;           type: 'nodejs:\{\{version:nodejs:latest}}'
> &#x20;           source:
> &#x20;             root: "/"
> &#x20;           crons:
> &#x20;             mycommand:
> &#x20;               spec: 'H \* \* \* \*'
> &#x20;               commands:
> &#x20;                 start: sleep 60 && echo sleep-60-finished && date
> &#x20;                 stop: killall sleep
> &#x20;               shutdown\_timeout: 18

> [tab] Composable image
>
> &#x20;       applications:
> &#x20;         \<APP\_NAME>:
> &#x20;           type: "composable:\{\{version:composable:latest}}"
> &#x20;           stack:
> &#x20;             runtimes: \[ 'nodejs:\{\{version:nodejs:latest}}' ]
> &#x20;           source:
> &#x20;             root: "/"
> &#x20;           crons:
> &#x20;             mycommand:
> &#x20;               spec: 'H \* \* \* \*'
> &#x20;               commands:
> &#x20;                 start: sleep 60 && echo sleep-60-finished && date
> &#x20;                 stop: killall sleep
> &#x20;               shutdown\_timeout: 18

In this example configuration, the crons `spec` key uses the `H` syntax.

### Single-runtime image: Example cron jobs

> [tab] Drupal
>
> &#x20;       applications:
> &#x20;         \# The name of the app container. Must be unique within a project.
> &#x20;         myapp:
> &#x20;           \# The location of the application's code.
> &#x20;           type: 'php:\{\{version:php:latest}}'
> &#x20;           crons:
> &#x20;             \# Run Drupal's cron tasks every 19 minutes.
> &#x20;             drupal:
> &#x20;               spec: '\*/19 \* \* \* \*'
> &#x20;               commands:
> &#x20;                 start: 'cd web ; drush core-cron'
> &#x20;             \# But also run pending queue tasks every 7 minutes.
> &#x20;             \# Use an odd number to avoid running at the same time as the \`drupal\` cron.
> &#x20;             drush-queue:
> &#x20;               spec: '\*/7 \* \* \* \*'
> &#x20;               commands:
> &#x20;                 start: 'cd web ; drush queue-run aggregator\_feeds'
> &#x20;    &#x20;

> [tab] Ruby on Rails
>
> &#x20;       applications:
> &#x20;         \# The name of the app container. Must be unique within a project.
> &#x20;         myapp:
> &#x20;           \# The location of the application's code.
> &#x20;           type: 'ruby:\{\{version:ruby:latest}}'
> &#x20;           crons:
> &#x20;             \# Execute a rake script every 19 minutes.
> &#x20;             ruby:
> &#x20;               spec: '\*/19 \* \* \* \*'
> &#x20;               commands:
> &#x20;                 start: 'bundle exec rake some:task'
> &#x20;    &#x20;

> [tab] Laravel
>
> &#x20;       applications:
> &#x20;         \# The name of the app container. Must be unique within a project.
> &#x20;         myapp:
> &#x20;           \# The location of the application's code.
> &#x20;           type: 'php:\{\{version:php:latest}}'
> &#x20;           crons:
> &#x20;             \# Run Laravel's scheduler every 5 minutes.
> &#x20;             scheduler:
> &#x20;               spec: '\*/5 \* \* \* \*'
> &#x20;               commands:
> &#x20;                 start: 'php artisan schedule:run'
> &#x20;    &#x20;

> [tab] Symfony
>
> &#x20;       applications:
> &#x20;         \# The name of the app container. Must be unique within a project.
> &#x20;         myapp:
> &#x20;           \# The location of the application's code.
> &#x20;           type: 'php:\{\{version:php:latest}}'
> &#x20;           crons:
> &#x20;             \# Take a backup of the environment every day at 5:00 AM.
> &#x20;             snapshot:
> &#x20;               spec: 0 5 \* \* \*
> &#x20;               commands:
> &#x20;                 start: |
> &#x20;                   \# Only run for the production environment, aka main branch
> &#x20;                   if \[ "\$PLATFORM\_ENVIRONMENT\_TYPE" = "production" ]; then
> &#x20;                       croncape symfony ...
> &#x20;                   fi
> &#x20;    &#x20;

### Composable image: Example cron jobs

> [tab] Drupal
>
> &#x20;       applications:
> &#x20;         \# The name of the app container. Must be unique within a project.
> &#x20;         myapp:
> &#x20;           \# The location of the application's code.
> &#x20;           type: "composable:\{\{version:composable:latest}}"
> &#x20;           stack:
> &#x20;             runtimes: \[ "php\@8.4" ]
> &#x20;           crons:
> &#x20;             \# Run Drupal's cron tasks every 19 minutes.
> &#x20;             drupal:
> &#x20;               spec: '\*/19 \* \* \* \*'
> &#x20;               commands:
> &#x20;                 start: 'cd web ; drush core-cron'
> &#x20;             \# But also run pending queue tasks every 7 minutes.
> &#x20;             \# Use an odd number to avoid running at the same time as the \`drupal\` cron.
> &#x20;             drush-queue:
> &#x20;               spec: '\*/7 \* \* \* \*'
> &#x20;               commands:
> &#x20;                 start: 'cd web ; drush queue-run aggregator\_feeds'

> [tab] Ruby on Rails
>
> &#x20;       applications:
> &#x20;         \# The name of the app container. Must be unique within a project.
> &#x20;         myapp:
> &#x20;           \# The location of the application's code.
> &#x20;           type: "composable:\{\{version:composable:latest}}"
> &#x20;           stack:
> &#x20;             runtimes: \[ "ruby@\{\{version:ruby:latest}}" ]
> &#x20;           crons:
> &#x20;             \# Execute a rake script every 19 minutes.
> &#x20;             ruby:
> &#x20;               spec: '\*/19 \* \* \* \*'
> &#x20;               commands:
> &#x20;                 start: 'bundle exec rake some:task'

> [tab] Laravel
>
> &#x20;       applications:
> &#x20;         \# The name of the app container. Must be unique within a project.
> &#x20;         myapp:
> &#x20;           \# The location of the application's code.
> &#x20;           type: "composable:\{\{version:composable:latest}}"
> &#x20;           stack:
> &#x20;             runtimes: \[ "php\@8.4" ]
> &#x20;           crons:
> &#x20;             \# Run Laravel's scheduler every 5 minutes.
> &#x20;             scheduler:
> &#x20;               spec: '\*/5 \* \* \* \*'
> &#x20;               commands:
> &#x20;                 start: 'php artisan schedule:run'

> [tab] Symfony
>
> &#x20;       applications:
> &#x20;         \# The name of the app container. Must be unique within a project.
> &#x20;         myapp:
> &#x20;           \# The location of the application's code.
> &#x20;           type: "composable:\{\{version:composable:latest}}"
> &#x20;           stack:
> &#x20;             runtimes: \[ "php\@8.4" ]
> &#x20;           crons:
> &#x20;             \# Take a backup of the environment every day at 5:00 AM.
> &#x20;             snapshot:
> &#x20;               spec: 0 5 \* \* \*
> &#x20;               commands:
> &#x20;                 start: |
> &#x20;                   \# Only run for the production environment, aka main branch
> &#x20;                   if \[ "\$PLATFORM\_ENVIRONMENT\_TYPE" = "production" ]; then
> &#x20;                       croncape symfony ...
> &#x20;                   fi

### Conditional crons

If you want to set up customized cron schedules depending on the environment type,
define conditional crons.
To do so, use a configuration similar to the following:

> [tab] Single-runtime image
>
> &#x20;       applications:
> &#x20;         myapp:
> &#x20;           type: 'php:\{\{version:php:latest}}'
> &#x20;           source:
> &#x20;             root: "/"
> &#x20;           crons:
> &#x20;             update:
> &#x20;               spec: '0 0 \* \* \*'
> &#x20;               commands:
> &#x20;                 start: |
> &#x20;                   if \[ "\$PLATFORM\_ENVIRONMENT\_TYPE" = production ]; then
> &#x20;                     upsun source-operation:run update --no-wait --yes
> &#x20;                   fi

> [tab] Composable image
>
> &#x20;       applications:
> &#x20;         myapp:
> &#x20;           type: "composable:\{\{version:composable:latest}}"
> &#x20;           source:
> &#x20;             root: "/"
> &#x20;           stack:
> &#x20;             runtimes: \[ "php\@8.4" ]
> &#x20;           crons:
> &#x20;             update:
> &#x20;               spec: '0 0 \* \* \*'
> &#x20;               commands:
> &#x20;                 start: |
> &#x20;                   if \[ "\$PLATFORM\_ENVIRONMENT\_TYPE" = production ]; then
> &#x20;                     upsun source-operation:run update --no-wait --yes
> &#x20;                   fi

### Cron job timing

The minimum time between cron jobs being triggered is 5 minutes.

For each app container, only one cron job can run at a time.
If a new job is triggered while another is running, the new job is paused until the other completes.

To minimize conflicts, a random offset is applied to all triggers.
The offset is a random number of seconds up to 20 minutes or the cron frequency, whichever is smaller.

Crons are also paused while activities such as [backups](https://developer.upsun.com/docs/environments/backup.md) are running.
The crons are queued to run after the other activity finishes.

To run cron jobs in a timezone other than UTC, set the `timezone` property property as described for the image type ([single-runtime image](https://developer.upsun.com/docs/configure-apps/app-reference/single-runtime-image.md#primary-application-properties) or
[composable image](https://developer.upsun.com/docs/configure-apps/app-reference/composable-image.md#primary-application-properties)).

To run cron jobs in a timezone other than UTC, set the [timezone property](https://developer.upsun.com/docs/configure-apps/timezone.md).

### Paused crons

[Preview environments](https://developer.upsun.com/docs/glossary.md#preview-environment) are often used for a limited time and then abandoned.
While it's useful for environments under active development to have scheduled tasks,
unused environments don't need to run cron jobs.
To minimize unnecessary resource use,
crons on environments with no deployments are paused.

This affects all preview environments *and* production environments that do not yet have a domain attached to them.

Such environments with deployments within 14 days have crons with the status `running`.
If there haven't been any deployments within 14 days, the status is `paused`.

You can see the status in the Console
or using the CLI by running `upsun environment:info` and looking under `deployment_state`.

#### Restarting paused crons

If the crons on your preview environment are paused but you're still using them,
you can push changes to the environment or redeploy it.

To restart crons without changing anything:

> [tab] In the Console
>
> 1. In the Console, navigate to your project.
> 2. Open the environment where you'd like the crons to run.
> 3. Click `Redeploy` next to the cron status of `Paused`.

> [tab] Using the CLI
>
> Run the following command:
>
> ```bash theme={null}
> upsun redeploy
> ```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.