A cron job is a time-based scheduling mechanism you can attach to your service. It allows you to automate the execution of CLI commands at predefined intervals. Cron jobs are commonly used for recurring tasks like database cleanups or batch processing.
To configure a cron job in your code editor, use the following variables in the service's configuration as code file.
# service: ...
cron_jobs:
- name: backup
schedule: '0 * * * *'
command: php backup-database.php
The name of the cron job must use snake_case notation. In addition, cron jobs within a service cannot share the same name.
The schedule that defines when the cron job is executed. It consists of five fields representing the time and date when the job should run.
* * * * *
| | | | |
| | | | +----- Day of the week (0 - 7) (Sunday = 0 or 7)
| | | +------- Month (1 - 12)
| | +--------- Day of the month (1 - 31)
| +----------- Hour (0 - 23)
+------------- Minute (0 - 59)
Each field can be:
1
)1,4
)1-4
)*
, which means anyThe combination of these fields allows for a wide range of scheduling options. Some examples:
* * * * *
-> Every minute45 23 * * 6
-> Every Saturday at 23:45 (11:45 PM)0 9 * * 1,4
-> Every Monday and Thursday at 09:00 (9:00 AM)0 10,22 * * 1-5
-> Every weekday (Mon-Fri) at 10:00 (10:00 AM) and 22:00 (10:00 PM)* * * * *
The CLI command to execute.
Example:
php backup_database.php
The timezone that is used to evaluate the schedule.
For some timezones, daylight saving can cause cron jobs to run or not run unexpectedly. When a time occurs twice (such as when clocks go backward), a cron job might observe execution anomalies. If your cron job requires a specific cadence, consider choosing a timezone that does not observe daylight saving time, like UTC.
Examples:
Etc/UTC
Europe/Brussels
America/New_York
Asia/Shanghai
Etc/UTC
The number of times the execution of the cron job is automatically retried after a failure.
0
5
0
The number of seconds after which the execution of the cron job times out.
30
If the execution of the cron job doesn't complete within this timeframe, the execution is marked as failed. The deadline must be a duration in seconds, terminated by an s
.
15s
1800s