← 返回
未分类 中文

Node Cron

Node.js cron job scheduling with the `cron` npm package. Use when the user needs to schedule recurring tasks, create cron jobs, validate cron expressions, se...
Node.js 中使用 `cron` npm 包进行定时任务调度。适用于需要安排循环任务、创建 cron 任务、校验 cron 表达式的场景。
openlark openlark 来源
未分类 clawhub v1.0.0 1 版本 100000 Key: 无需
★ 0
Stars
📥 279
下载
💾 0
安装
1
版本
#latest

概述

Node Cron

Use the cron npm package (npm install cron) for second-precision scheduled tasks in Node.js.

Triggers

cron job, node-cron, schedule task, cron expression, cron pattern, scheduled job, npm install cron, CronJob, CronTime.

Quick Start

import { CronJob } from 'cron';

const job = new CronJob(
  '0 */5 * * * *',  // every 5 minutes
  () => console.log('Running every 5 minutes'),
  null,             // onComplete
  true,             // start immediately
  'Asia/Shanghai'   // timeZone
);

Or use the object form:

const job = CronJob.from({
  cronTime: '0 0 9 * * 1',    // 9am every Monday
  onTick: () => sendReport(),
  start: true,
  timeZone: 'Asia/Shanghai'
});

job.stop();   // halt
job.start();  // resume

Cron Patterns

The library uses 6 fields (second-precision), unlike standard 5-field Unix cron:

second  minute  hour  day-of-month  month  day-of-week
  0-59   0-59   0-23     1-31       1-12     0-7

Syntax

| Token | Meaning |

|-------|---------|

| * | Any value (every second/minute/etc.) |

| 1,3,5 | List of values |

| 1-5 | Range (inclusive) |

| */5 | Every N steps |

Names

Use first 3 letters for month/day-of-week (case-insensitive):

"jan,mar,dec", "mon,wed,fri"

Day-of-week: 0 or 7 is Sunday.

Common Examples

| Expression | Meaning |

|------------|---------|

| /10 | Every 10 seconds |

| 0 * | Every minute on the second |

| 0 0 | Every hour |

| 0 0 9 * | Daily at 9:00 AM |

| 0 30 9 1-5 | 9:30 AM Mon-Fri |

| 0 0 0 1 | Midnight on the 1st of every month |

CronJob Class

Constructor Parameters

| Param | Required | Description |

|-------|----------|-------------|

| cronTime | [OK] | Cron pattern string, Date, or Luxon DateTime |

| onTick | [OK] | Callback function |

| onComplete | | Called when job stops via job.stop() |

| start | | Auto-start. Default false |

| timeZone | | IANA zone string (e.g. 'Asia/Shanghai') |

| context | | this context for onTick |

| runOnInit | | Fire onTick immediately on init |

| waitForCompletion | | If true, skip ticks while callback is still running |

| errorHandler | | Catch exceptions in onTick |

| name | | Job identifier for debugging |

| threshold | | Missed-deadline tolerance in ms. Default 250 |

Do NOT pass utcOffset together with timeZone -- they conflict.

Key Methods

  • CronJob.from(obj) (static) -- Create with named params
  • job.start() / job.stop() -- Start/stop
  • job.nextDate() -- Next execution as Luxon DateTime
  • job.nextDates(n) -- Array of next N dates
  • job.lastDate() -- Last execution date
  • job.setTime(cronTime) -- Change schedule
  • job.addCallback(fn) -- Add another onTick callback

Read-Only Properties

  • job.isActive -- Whether job is running
  • job.isCallbackRunning -- Whether onTick is currently executing

Standalone Utilities

import * as cron from 'cron';

// When will this cron expression fire next?
const dt = cron.sendAt('0 0 * * *');
console.log(dt.toISO());

// How many ms until next execution?
const ms = cron.timeout('0 0 * * *');

// Validate an expression
const { valid, error } = cron.validateCronExpression('0 0 * * *');

Gotchas

  • Month is 1-12, not 0-11. Upgrade from v2 needs +1 on all month values.
  • Day-of-week 0 = Sunday (7 also works).
  • 6 fields (with seconds) -- unlike standard 5-field Unix cron.
  • Use waitForCompletion: true for async callbacks to prevent overlap.

Async onTick

Wrap async work directly -- waitForCompletion prevents overlapping runs:

const job = CronJob.from({
  cronTime: '*/30 * * * * *',
  onTick: async () => {
    await fetchData();
    await processData();
  },
  waitForCompletion: true,
  start: true
});

Full API Reference

See references/api_reference.md for the complete API documentation including CronTime class, all method signatures, and migration notes.

版本历史

共 1 个版本

  • v1.0.0 当前
    2026-05-08 04:05 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

content-creation

Toutiao Graphic Publisher

openlark
通过浏览器自动化在头条发布图文内容,支持智能排版、自动生成热门标签等功能。
★ 2 📥 1,037
dev-programming

Mcporter

steipete
使用 mcporter CLI 直接列出、配置、认证及调用 MCP 服务器/工具(支持 HTTP 或 stdio),涵盖临时服务器、配置编辑及 CLI/类型生成功能。
★ 198 📥 68,277
dev-programming

Github

steipete
使用 `gh` CLI 与 GitHub 交互,通过 `gh issue`、`gh pr`、`gh run` 和 `gh api` 管理议题、PR、CI 运行及高级查询。
★ 686 📥 331,330