Le espressioni cron sono il linguaggio universale per pianificare attività ricorrenti. Ogni piattaforma serverless ha le sue peculiarità.
Basi delle espressioni cron
Un'espressione cron standard ha 5 campi:
┌───────────── minute (0-59)
│ ┌───────────── hour (0-23)
│ │ ┌───────────── day of month (1-31)
│ │ │ ┌───────────── month (1-12)
│ │ │ │ ┌───────────── day of week (0-6, Sun=0)
│ │ │ │ │
* * * * *Caratteri speciali comuni:
*— qualsiasi valore*/5— ogni 5 unità1,3,5— valori specifici1-5— intervallo
Testa le tue espressioni cron con il nostro Cron Parser →
Piattaforma 1: GitHub Actions
GitHub Actions usa l'evento schedule.
Configurazione
# .github/workflows/scheduled.yml
name: Scheduled Job
on:
schedule:
- cron: '30 5 * * 1-5' # Weekdays at 5:30 AM UTC
- cron: '0 12 1 * *' # 1st of every month at noon UTC
jobs:
run:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Running scheduled task"Limitazioni
| Aspetto | Dettagli |
|---|---|
| Fuso orario | Solo UTC |
| Intervallo min | Ogni 5 minuti (*/5 * * * *) |
| Precisione | Possibile ritardo |
| Branch | Solo branch predefinito |
| Disattivazione | Dopo 60 giorni inattività |
Pianificazioni comuni
# Every 5 minutes
- cron: '*/5 * * * *'
# Every hour at minute 0
- cron: '0 * * * *'
# Daily at midnight UTC
- cron: '0 0 * * *'
# Daily at 9 AM Eastern (UTC-5, no DST)
- cron: '0 14 * * *'
# Weekdays at 8 AM UTC
- cron: '0 8 * * 1-5'
# Every Monday and Thursday at 6 PM UTC
- cron: '0 18 * * 1,4'
# First day of every month at midnight
- cron: '0 0 1 * *'Tip: GitHub Actions può ritardare.
Piattaforma 2: Vercel Cron Jobs
Configurazione in vercel.json.
Configurazione
// vercel.json
{
"crons": [
{
"path": "/api/daily-report",
"schedule": "0 8 * * *"
},
{
"path": "/api/cleanup",
"schedule": "0 0 * * 0"
}
]
}Route API
// app/api/daily-report/route.ts
import { NextResponse } from 'next/server';
export async function GET() {
// Your scheduled logic here
await sendDailyReport();
return NextResponse.json({ ok: true });
}
// Vercel recommends setting maxDuration for long tasks
export const maxDuration = 60; // secondsLimitazioni
| Piano | Interv. min | Max Cron Jobs | Limite esecuzione |
|---|---|---|---|
| Hobby (free) | Once per day | 2 | 10s |
| Pro | Once per hour | 40 | 60s (default) |
| Enterprise | Once per minute | 100 | 900s |
Important: Vercel usa UTC. Hobby: 1/giorno.
Piattaforma 3: Cloudflare Workers
Configurazione in wrangler.toml.
Configurazione
# wrangler.toml
name = "my-worker"
main = "src/index.ts"
[triggers]
crons = [
"*/5 * * * *", # Every 5 minutes
"0 12 * * 1-5", # Weekdays at noon UTC
"0 0 1 * *" # First of every month
]Codice Worker
// src/index.ts
export default {
async scheduled(
controller: ScheduledController,
env: Env,
ctx: ExecutionContext
) {
// controller.cron contains the cron pattern that triggered
switch (controller.cron) {
case '*/5 * * * *':
await doHealthCheck(env);
break;
case '0 12 * * 1-5':
await sendReport(env);
break;
}
},
};Limitazioni
| Aspetto | Dettagli |
|---|---|
| Fuso orario | Solo UTC |
| Intervallo min | 1x al minuto |
| Max cron triggers | 3 per Worker (gratis) |
| CPU | 10ms/30s |
| Precisione | Molto affidabile |
Confronto piattaforme
| Funzione | GitHub Actions | Vercel Cron | CF Workers |
|---|---|---|---|
| Timezone | UTC | UTC | UTC |
| Min interval (free) | 5 min | 1/day | 1 min |
| Min interval (paid) | 5 min | 1/min | 1 min |
| Reliability | Medium (can delay) | High | Very high |
| Max execution time | 6 hours | 10s-900s | 10ms-30s |
| Config location | .github/workflows/ | vercel.json | wrangler.toml |
| Trigger handler | Workflow YAML | API route | scheduled() event |
Trappola fusi orari
Tutte e tre usano esclusivamente UTC.
| Ora locale | Offset UTC | Cron (9h locale) |
|---|---|---|
| US Eastern (EST) | UTC-5 | 0 14 * * * |
| US Pacific (PST) | UTC-8 | 0 17 * * * |
| Central Europe (CET) | UTC+1 | 0 8 * * * |
| China (CST) | UTC+8 | 0 1 * * * |
| Japan (JST) | UTC+9 | 0 0 * * * |
L'ora legale cambia gli offset UTC.
10 espressioni cron pronte
Pianificazioni più comuni:
# Every 5 minutes
*/5 * * * *
# Every hour at :00
0 * * * *
# Every day at midnight UTC
0 0 * * *
# Every day at noon UTC
0 12 * * *
# Weekdays (Mon-Fri) at 9 AM UTC
0 9 * * 1-5
# Every Monday at 8 AM UTC
0 8 * * 1
# Every 6 hours
0 */6 * * *
# First day of every month at midnight
0 0 1 * *
# Every 15 minutes during business hours (8-17 UTC)
*/15 8-17 * * 1-5
# Last day of month at 11 PM UTC (approximate)
0 23 28-31 * *Valida espressioni cron con Cron Parser →
Domande frequenti
Quale fuso orario per GitHub Actions?
Sempre UTC.
Intervallo min Vercel Cron?
Hobby: 1/giorno, Pro: 1/ora, Enterprise: 1/minuto.
Cloudflare Workers ogni secondo?
No. Granularità min: 1 minuto.