
Research
Security News
The Growing Risk of Malicious Browser Extensions
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
node-cron is a task scheduler for Node.js that allows you to schedule jobs (functions) to run at specific times or intervals using cron syntax. It is useful for automating repetitive tasks such as backups, email notifications, and data processing.
Basic Scheduling
This feature allows you to schedule a task to run at specific intervals using cron syntax. In this example, the task runs every minute.
const cron = require('node-cron');
cron.schedule('* * * * *', () => {
console.log('Running a task every minute');
});
Specific Time Scheduling
This feature allows you to schedule a task to run at a specific time. In this example, the task runs every day at midnight.
const cron = require('node-cron');
cron.schedule('0 0 * * *', () => {
console.log('Running a task every day at midnight');
});
Task Stopping
This feature allows you to stop a scheduled task. In this example, the task is scheduled to run every minute but is stopped immediately after being scheduled.
const cron = require('node-cron');
let task = cron.schedule('* * * * *', () => {
console.log('Running a task every minute');
});
task.stop();
Task Starting
This feature allows you to start a scheduled task. In this example, the task is scheduled to run every minute and is started explicitly.
const cron = require('node-cron');
let task = cron.schedule('* * * * *', () => {
console.log('Running a task every minute');
});
task.start();
Task Destruction
This feature allows you to completely destroy a scheduled task, removing it from the scheduler. In this example, the task is scheduled to run every minute but is destroyed immediately after being scheduled.
const cron = require('node-cron');
let task = cron.schedule('* * * * *', () => {
console.log('Running a task every minute');
});
task.destroy();
node-schedule is another task scheduler for Node.js that uses a more flexible syntax compared to cron. It allows for more complex scheduling patterns and supports both cron-like syntax and JavaScript date objects. It is more feature-rich but also more complex to use compared to node-cron.
agenda is a light-weight job scheduling library for Node.js that uses MongoDB for persistence. It is designed for long-running jobs and supports job prioritization, job concurrency, and job locking. It is more suitable for applications that require persistent job storage and more advanced job management features compared to node-cron.
bree is a job scheduler for Node.js that uses worker threads to run jobs in parallel. It supports cron syntax and provides features like graceful shutdown, job retries, and job concurrency. It is more suitable for applications that require parallel job execution and advanced job management features compared to node-cron.
The node-cron module is tiny task scheduler in pure JavaScript for node.js based on GNU crontab. This module allows you to schedule task in node.js using full crontab syntax.
Install node-cron using npm:
npm install --save node-cron
Import node-cron and schedule a task:
const cron = require('node-cron');
cron.schedule('* * * * *', () => {
console.log('running a task every minute');
});
import cron from 'node-cron';
cron.schedule('* * * * *', () => {
console.log('running a task every minute');
});
This is a quick reference to cron syntax and also shows the options supported by node-cron.
# ┌────────────── second (optional)
# │ ┌──────────── minute
# │ │ ┌────────── hour
# │ │ │ ┌──────── day of month
# │ │ │ │ ┌────── month
# │ │ │ │ │ ┌──── day of week
# │ │ │ │ │ │
# │ │ │ │ │ │
# * * * * * *
field | value |
---|---|
second | 0-59 |
minute | 0-59 |
hour | 0-23 |
day of month | 1-31 |
month | 1-12 (or names) |
day of week | 0-7 (or names, 0 or 7 are sunday) |
Feel free to submit issues and enhancement requests here.
In general, we follow the "fork-and-pull" Git workflow.
NOTE: Be sure to merge the latest from "upstream" before making a pull request!
Please do not contribute code you did not write yourself, unless you are certain you have the legal ability to do so. Also ensure all contributed code can be distributed under the ISC License.
This project exists thanks to all the people who contribute.
Thank you to all our backers! 🙏 [Become a backer]
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]
node-cron is under ISC License.
FAQs
A Lightweight Task Scheduler for Node.js
The npm package node-cron receives a total of 950,013 weekly downloads. As such, node-cron popularity was classified as popular.
We found that node-cron demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer 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 uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
Research
Security News
An in-depth analysis of credential stealers, crypto drainers, cryptojackers, and clipboard hijackers abusing open source package registries to compromise Web3 development environments.
Security News
pnpm 10.12.1 introduces a global virtual store for faster installs and new options for managing dependencies with version catalogs.