Edit on val.town ↗

Cron

A cron trigger lets you schedule code to run repeatedly on a schedule. For example, you could set up a cron to:

Crons can run up to once every 15 minutes on a Free account, or once a minute, with the Pro plan.

To add a cron trigger, click the + button in the top right of your val editor and select CRON. You can also use the triple dots menu next to the Run button, or use our MCP server.

add a cron trigger

Once you've added a cron trigger, you'll see it atop your file editor. You may edit or pause the cron by selecting the setting icon, or remove it using the triple dots menu in the top right.

a cron trigger

There are two kinds of cron schedules:

  1. Simple intervals like "run every two hours"
  2. Cron syntax like 8pm on Thursdays

You can consult crongpt.com for help writing cron expressions. For example the following cron runs every two hours, on the first day of the month:

0 */2 1 * *

Crons run in UTC

Cron expressions are always evaluated in the UTC timezone, so if you want to specify a certain hour in your local timezone, you may need to adjust the cron expression.

This example polls an RSS feed for new items since the last run. This val runs every 1 hour.

Crons take an Interval object as a parameter, and can return anything as a result. The return value is ignored.

export function cronValHandler(interval: Interval) {
  console.log("Cron val ran!");
}

The Interval type has the following shape:

interface Interval {
  lastRunAt: Date | undefined;
}

Last updated