cron-jobs

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Vercel Cron Jobs

Vercel Cron Jobs

You are an expert in Vercel Cron Jobs — scheduled serverless function invocations configured in
vercel.json
.
你是Vercel Cron Jobs的专家——这是一种在
vercel.json
中配置的定时无服务器函数调用服务。

Configuration

配置

Cron jobs are defined in the
crons
array of
vercel.json
:
json
{
  "crons": [
    {
      "path": "/api/cron/daily-digest",
      "schedule": "0 8 * * *"
    }
  ]
}
定时任务定义在
vercel.json
crons
数组中:
json
{
  "crons": [
    {
      "path": "/api/cron/daily-digest",
      "schedule": "0 8 * * *"
    }
  ]
}

Key Rules

核心规则

  1. Path must be an API route — the
    path
    field must point to a serverless function endpoint (e.g.,
    /api/cron/...
    )
  2. Schedule uses standard cron syntax — five-field format:
    minute hour day-of-month month day-of-week
  3. Verify the request origin — always check the
    Authorization
    header matches
    CRON_SECRET
    :
ts
// app/api/cron/route.ts
export async function GET(request: Request) {
  const authHeader = request.headers.get("authorization");
  if (authHeader !== `Bearer ${process.env.CRON_SECRET}`) {
    return new Response("Unauthorized", { status: 401 });
  }
  // ... your scheduled logic
  return Response.json({ ok: true });
}
  1. Hobby plan limits — max 2 cron jobs, minimum interval of once per day
  2. Pro plan — up to 40 cron jobs, minimum interval of once per minute
  3. Max duration — cron-triggered functions follow normal function duration limits
  1. 路径必须是API路由——
    path
    字段必须指向无服务器函数端点(例如:
    /api/cron/...
  2. 调度使用标准Cron语法——五字段格式:
    分钟 小时 日期 月份 星期
  3. 验证请求来源——始终检查
    Authorization
    头是否与
    CRON_SECRET
    匹配:
ts
// app/api/cron/route.ts
export async function GET(request: Request) {
  const authHeader = request.headers.get("authorization");
  if (authHeader !== `Bearer ${process.env.CRON_SECRET}`) {
    return new Response("Unauthorized", { status: 401 });
  }
  // ... 你的定时任务逻辑
  return Response.json({ ok: true });
}
  1. 免费版限制——最多2个定时任务,最小执行间隔为每天一次
  2. 专业版——最多40个定时任务,最小执行间隔为每分钟一次
  3. 最长执行时长——定时触发的函数遵循常规函数的时长限制

Common Patterns

常见模式

  • Daily digest:
    "0 8 * * *"
    (8:00 AM UTC daily)
  • Every hour:
    "0 * * * *"
  • Every 5 minutes (Pro):
    "*/5 * * * *"
  • Weekdays only:
    "0 9 * * 1-5"
  • 每日摘要
    "0 8 * * *"
    (UTC时间每天上午8点)
  • 每小时一次
    "0 * * * *"
  • 每5分钟一次(专业版)
    "*/5 * * * *"
  • 仅工作日执行
    "0 9 * * 1-5"

Debugging

调试

  • Check deployment logs for cron execution results
  • Use
    vercel logs --follow
    to watch cron invocations in real time
  • Cron jobs only run on production deployments, not preview deployments
  • 查看部署日志获取定时任务执行结果
  • 使用
    vercel logs --follow
    实时监控定时任务调用情况
  • 定时任务仅在生产环境部署中运行,预览部署不会执行

References

参考资料