node-schedule /node-schedule
A cron-like and not-cron-like job scheduler for Node.
★ 9,197⑂ 501👁 105⑃ 281● 482▤ 21
+0
日增长
9,197
stars
501
forks
105
watchers
281
prs
482
issues
21
releases
README
Node Schedule
Node Schedule is a flexible cron-like and not-cron-like job scheduler for Node.js.
It allows you to schedule jobs (arbitrary functions) for execution at
specific dates, with optional recurrence rules. It only uses a single timer
at any given time (rather than reevaluating upcoming jobs every second/minute).
Node 6+ is supported.
Overview
Node Schedule is for time-based scheduling, not interval-based scheduling.
While you can easily bend it to your will, if you only want to do something like
"run this function every 5 minutes", toad-scheduler would be a better choice. But if you want to, say, "run this function at the :20
and :50 of every hour on the third Tuesday of every month," you'll find that
Node Schedule suits your needs better. Additionally, Node Schedule has Windows
support, unlike true cron, since the node runtime is now fully supported.
Note that Node Schedule is designed for in-process scheduling, i.e. scheduled jobs
will only fire as long as your script is running, and the schedule will disappear
when execution completes. If you need to schedule jobs that will persist even when
your script isn't running, consider using actual [cron].
In case you need durable jobs that persist across restarts and lock system compatible with multi-node deployments,
try agenda or bree.
Usage
Installation
You can install using npm.
npm install node-schedule
Jobs and Scheduling
Every scheduled job in Node Schedule is represented by a Job object. You can
create jobs manually, then execute the schedule() method to apply a schedule,
or use the convenience function scheduleJob() as demonstrated below.
Job objects are EventEmitters, and emit the following events:
A run event after each execution.
A scheduled event each time they're scheduled to run.
A canceled event when an invocation is canceled before it's executed.
Note that canceled is the single-L American spelling.
An error event when a job invocation triggered by a schedule throws or returns
a rejected Promise.
A success event when a job invocation triggered by a schedule returns successfully or
returns a resolved Promise. In any case, the success event receives the value returned
by the callback or in case of a promise, the resolved value.
(Both the scheduled and canceled events receive a JavaScript date object as
a parameter).
Note that jobs are scheduled the first time immediately, so if you create a job
using the scheduleJob() convenience method, you'll miss the first scheduled
event, but you can query the invocation manually (see below).
Cron-style Scheduling
The cron format consists of:
┬ ┬ ┬ ┬ ┬ ┬
│ │ │ │ │ │
│ │ │ │ │ └ day of week (0 - 7) (0 or 7 is Sun)
│ │ │ │ └───── month (1 - 12)
│ │ │ └────────── day of month (1 - 31)
│ │ └─────────────── hour (0 - 23)
│ └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59Top issues
#340Usage with Heroku29 comments · 2017-04-12#217Timezone support28 comments · 2015-12-04#322Schedule stops running25 comments · 2017-01-12#494Schedule stops running after some days20 comments · 2019-05-15#351Setting up Job to run for 8 hours but times out after an hour16 comments · 2017-05-20#431Scheduled job tries to 'catch up'15 comments · 2018-03-19#428Question: Running a cron task once15 comments · 2018-03-09#313schedule.scheduledJobs 15 comments · 2016-12-01
Latest release
v2.1.2 v2.1.2 2026-10-24Patch Changes - bef41ec: Cancel the polling interval in gracefulShutdown() once all running jobs have finished, so the process can exit (752)