
Research
Security News
Malicious PyPI Package Exploits Deezer API for Coordinated Music Piracy
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
node-schedule
Advanced tools
The node-schedule npm package 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 is suitable for building job schedulers, task reminders, or any other automation based on time events.
One-time jobs
Schedule a job to be executed once at a specific date and time.
const schedule = require('node-schedule');
const date = new Date(2023, 3, 21, 5, 30, 0);
const job = schedule.scheduleJob(date, function() {
console.log('This job is run once at the specified date.');
});
Recurring jobs using cron-style scheduling
Schedule a job to be executed on a recurring basis using a cron string to specify the frequency.
const schedule = require('node-schedule');
const job = schedule.scheduleJob('*/5 * * * *', function() {
console.log('This job runs every 5 minutes.');
});
Recurring jobs using object literal syntax
Schedule a job using a more human-readable object literal syntax for setting the schedule.
const schedule = require('node-schedule');
const job = schedule.scheduleJob({hour: 14, minute: 30, dayOfWeek: 1}, function() {
console.log('This job runs at 2:30pm every Monday.');
});
Canceling jobs
Cancel a scheduled job before it is executed.
const schedule = require('node-schedule');
const job = schedule.scheduleJob('*/5 * * * *', function() {
console.log('This job will be canceled.');
});
job.cancel();
The 'cron' package is similar to node-schedule and is used for scheduling jobs to run at specific times or intervals. It uses the Unix cron string format to define schedules and is widely used for its simplicity and familiarity to those with Unix/Linux experience.
Agenda is a more feature-rich job scheduling library for Node.js. It supports persistence with MongoDB, which allows scheduled jobs to be stored and run across server restarts. It also offers more control over job queuing and locking, making it suitable for more complex scheduling needs.
Bull is a Redis-based queue system for Node.js. It is often used for job scheduling as well as for processing distributed jobs in the background. It offers advanced features like repeatable jobs, rate limiting, and job prioritization, making it a good choice for high-performance job processing.
Bree is a job scheduler for Node.js that supports cron, dates, ms (milliseconds), later, and human-friendly strings for job scheduling. It uses workers for parallel job processing and includes support for job timeouts and graceful cancellation. It's a modern alternative with a focus on reliability and concurrency.
node-schedule is a cron-like and not-cron-like job scheduler for Node. 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), and is MIT-licensed (see below).
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.
Note that node-schedule is 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 the 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 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. Also note that canceled
is the single-L American spelling.
Say you very specifically want a function to execute at 5:30am on December 21, 2012.
var schedule = require('node-schedule');
var date = new Date(2012, 11, 21, 5, 30, 0);
var j = schedule.scheduleJob(date, function(){
console.log('The world is going to end today.');
});
If you later rethink your decision, you can invalidate the job with the cancel()
method:
j.cancel();
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 = new schedule.RecurrenceRule();
rule.minute = 42;
var j = schedule.scheduleJob(rule, function(){
console.log('The answer to life, the universe, and everything!');
});
You can also use arrays to specify a list of acceptable values, and the Range
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 = new schedule.RecurrenceRule();
rule.dayOfWeek = [0, new schedule.Range(4, 6)];
rule.hour = 17;
rule.minute = 0;
var j = schedule.scheduleJob(rule, function(){
console.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 explicitly set 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 = schedule.scheduleJob({hour: 14, minute: 30, dayOfWeek: 0}, function(){
console.log('Time for tea!');
});
If you're a fan of cron, you can instead define your recurrence rules using a syntax similar to what you might write in your crontab. For example, the above examples rewritten using this style:
var schedule = require('node-schedule');
var j = schedule.scheduleJob('42 * * * *', function(){
console.log('The answer to life, the universe, and everything!');
});
And:
var j = schedule.scheduleJob('0 17 ? * 0,4-6', function(){
console.log('Today is recognized by Rebecca Black!');
});
Currently, W
(nearest weekday), L
(last day of month/week), and #
(nth weekday of the month) are not supported. Also, in the day-of-week field, 7 is currently not recognized as a legal value for Sunday (use 0). Most other features supported by popular cron implementations should work just fine.
It is also entirely possible at this point that constructing a cron string that can only exist in the past will cause an infinite loop. This is only possible if a year is specified. If the specified year is a number (i.e., not a range), node-schedule will perform a sanity check before attempting to schedule something in the past.
You can install using npm
in the usual way.
npm install node-schedule
Copyright (C) 2011 Matt Patenaude.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
FAQs
A cron-like and not-cron-like job scheduler for Node.
The npm package node-schedule receives a total of 1,310,492 weekly downloads. As such, node-schedule popularity was classified as popular.
We found that node-schedule demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.
Security News
Newly introduced telemetry in devenv 1.4 sparked a backlash over privacy concerns, leading to the removal of its AI-powered feature after strong community pushback.