Skip to main content
On this page

Cron

Deno.cron() is a Deno runtime API for scheduling JavaScript or TypeScript code to run on a recurring schedule, expressed using cron syntax. It ships with Deno itself, so the same code that runs locally can be deployed without changes.

Deno.cron is currently an unstable API. To use it locally with deno run, enable the --unstable-cron flag (or add "cron" to the unstable array in deno.json).

>_
deno run --unstable-cron main.ts

Deno.cron API reference

Defining a cron job Jump to heading

Deno.cron() takes a human-readable name, a schedule, and a handler function. The name identifies the cron job in logs, the schedule determines when the handler fires, and all times are in UTC.

Deno.cron("log-a-message", "* * * * *", () => {
  console.log("This runs once a minute.");
});

The schedule can be a standard 5-field cron expression or a structured object:

Deno.cron("hourly-task", { hour: { every: 1 } }, () => {
  console.log("This runs once an hour.");
});

Cron jobs must be registered at the top level of a module, before any server starts. Definitions nested inside request handlers, conditionals, or callbacks will not be picked up.

Retries and backoff Jump to heading

By default, failed handler invocations are not retried. Pass a backoffSchedule (an array of millisecond delays) to retry on failure:

Deno.cron(
  "retry-example",
  "* * * * *",
  { backoffSchedule: [1000, 5000, 10000] },
  () => {
    throw new Error("Will be retried up to three times.");
  },
);

Running cron jobs in production Jump to heading

Deno.cron keeps execution state in-memory in the Deno CLI, which means each process maintains its own independent set of cron tasks. For production workloads, Deno Deploy builds on top of this runtime API: it discovers your Deno.cron() definitions at deployment time, schedules and invokes them, handles retries, and surfaces runs in a dashboard — so you don't need to keep a long-running process up yourself.

Last updated on

Did you find what you needed?

Privacy policy