A cron-like and not-cron-like job scheduler for Node.
Announcement: Node Schedule is looking for add additional collaborators with commit access. If you are actively involved in open source, ping Tejas Manohar via email to express interest. Those who already contribute to the project are preferred.
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).
You can install using npm.
npm install node-schedule
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", you'll find
setInterval much easier to use,
and far more appropriate. 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.
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
EventEmitter's, and emit a
run event after each execution.
They also emit a
scheduled event each time they're scheduled to run, and a
canceled event when an invocation is canceled before it's executed (both events
first time immediately, so if you create a job using the
convenience method, you'll miss the first
scheduled event. Also note that
canceled is the single-L American spelling.
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 - 59, OPTIONAL)
Examples with the cron format:
var schedule = require'node-schedule';var j = schedulescheduleJob'42 * * * *'console.log'The answer to life, the universe, and everything!';;
var j = schedulescheduleJob'0 17 ? * 0,4-6'console.log'Today is recognized by Rebecca Black!';;
Execute a cron job every 5 Minutes = */5 * * * *
W (nearest weekday),
L (last day of month/week), and
# (nth weekday
of the month) are not supported. Most other features supported by popular cron
implementations should work just fine.
cron-parser is used to parse crontab instructions.
var schedule = require'node-schedule';var date = 2012 11 21 5 30 0;var j = schedulescheduleJobdateconsole.log'The world is going to end today.';;
You can invalidate the job with the
To use current data in the future you can use binding:
var schedule = require'node-schedule';var date = 2012 11 21 5 30 0;var x = 'Tada!';var j = schedulescheduleJobdateconsole.logy;bindnullx;x = 'Changing Data';
This will log 'Tada!' when the scheduled Job runs, rather than 'Changing Data', which x changes to immediately after scheduling.
You can build recurrence rules to specify when a job should recur. For instance, consider this rule, which executes the function every hour at 42 minutes after the hour:
var schedule = require'node-schedule';var rule = ;ruleminute = 42;var j = schedulescheduleJobruleconsole.log'The answer to life, the universe, and everything!';;
You can also use arrays to specify a list of acceptable values, and the
object to specify a range of start and end values, with an optional step parameter.
For instance, this will print a message on Thursday, Friday, Saturday, and Sunday at 5pm:
var rule = ;ruledayOfWeek = 0 4 6;rulehour = 17;ruleminute = 0;var j = schedulescheduleJobruleconsole.log'Today is recognized by Rebecca Black!';;
It's worth noting that the default value of a component of a recurrence rule is
null (except for seconds, which is 0 for familiarity with cron). If we did not
minute to 0 above, the message would have instead been logged at
5:00pm, 5:01pm, 5:02pm, ..., 5:59pm. Probably not what you want.
To make things a little easier, an object literal syntax is also supported, like in this example which will log a message every Sunday at 2:30pm:
var j = schedulescheduleJobhour: 14 minute: 30 dayOfWeek: 0console.log'Time for tea!';;
We'd love to get your contributions. Individuals making significant and valuable contributions are given commit-access to the project to contribute as they see fit.
Before jumping in, check out our Contributing page guide!
Copyright 2015 Matt Patenaude.
Licensed under the MIT License.