Cron jobs
Run a command from your web service's code on a schedule.
A cron job runs a command on a schedule, for example a nightly cleanup or an hourly report. Like a worker, it runs the code from one of your web services, using the build from that service's latest deploy.
Create a cron job
You need a web service in the project that has deployed at least once.
- On the project's Services page, click Add next to Cron Jobs.
- Fill in the form:
- Name
- Attach to service: the web service whose code the job runs.
- Command: for example
python cron_task.pyornode scripts/send-reports.js. For a Feather app, run one of its background jobs withfeather jobs runand the job's name, for examplefeather jobs run send_digest. - Schedule: see below.
- Timezone
- Machine: which of the project's machines it runs on. It must be in the same zone as its web service.
- Guaranteed memory: what each run is guaranteed. See Memory and sizing.
- Under Advanced options, Timeout (seconds) and Retries on failure.
- Price: what the cron job costs a month.
- Click Create cron job.
The command runs directly, not through a shell. To use shell features such as &&, wrap it: sh -c "…".
Schedule
Pick a frequency and the form builds the schedule for you:
| Frequency | Options |
|---|---|
| Every N minutes | 5, 10, 15 or 30 minutes |
| Every N hours | 1, 2, 4, 6, 8 or 12 hours |
| Daily at | a time on the hour |
| Weekly on | a day and a time on the hour |
| Monthly on | a day from 1 to 28 and a time on the hour |
| Custom | any standard five-field cron expression |
The form shows the resulting expression next to Schedule. The default is every hour, 0 * * * *.
Timezone
The schedule runs in the timezone you choose. The default is UTC. Choose from UTC and a short list of common timezones, such as America/New_York, Europe/London and Asia/Tokyo.
Timeout and retries
- Timeout (seconds): the longest a run may take before it's stopped. The default is 600 seconds (10 minutes). It can be from 10 seconds to 86,400 seconds (24 hours).
- Retries on failure: how many times a failed run is tried again, from 0 to 3. The default is 0.
You set both when you create the cron job. They can't be changed afterwards.
Runs that overlap
A cron job never runs twice at once. If a run is still going when the next one is due, the next one is skipped.
A run that can't start within 2 minutes of its time, for example while its machine restarts, is skipped.
Run now, pause and resume
In the cron job's Settings, under Cron control:
- Run now starts a run straight away.
- Pause stops the schedule. No runs start until you resume it.
- Resume turns the schedule back on.
Status
| Status | Meaning |
|---|---|
| Active | The schedule is on. |
| Paused | You paused the schedule. |
| Creating | The cron job is being set up. |
| Failed | The cron job couldn't be set up or changed. |
Results of each run
Each finished run appears on the cron job's Events page, for example "Scheduled run succeeded in 12s" or "Manual run failed", with the reason. Failed runs also appear under the bell if Resource failures is on in notifications.
After the first run, the Overview also shows Last execution: when it ran and whether it Succeeded or Failed. The Overview always shows the schedule, timezone, command, attached service, timeout and retries.
A cron job has no Logs page, and a run's output isn't kept. To keep a record, have the command write it to a database or to storage.
Updates from the web service
When the attached web service deploys, the cron job uses the new version from its next run.
Variables
A cron job gets the same variables as its web service, plus any you set for the cron job itself. See Environment variables.
Change a cron job
In the cron job's Settings, under General, you can change its Name, Command, Schedule, Timezone and Attached service, then click Update. Schedule changes take effect straight away. Change its memory under Scaling. The Use idle memory switch there saves straight away. See Use idle memory.
A cron job runs its web service's code, so it runs on a machine in that web service's zone. When the project has another machine there, its Settings have a Machine section to move it. See Machines.
Billing
A cron job holds its guaranteed memory on the machine and pays its service fee all the time, whether or not a run is going, and also while it's paused.
Delete a cron job
In the cron job's Settings, click Delete cron job in the Danger Zone and confirm. It can't be undone. Deleting the web service a cron job is attached to also deletes the cron job.