Gathering detailed insights and metrics for node-cron
Gathering detailed insights and metrics for node-cron
Gathering detailed insights and metrics for node-cron
Gathering detailed insights and metrics for node-cron
npm install node-cron
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
2,989 Stars
381 Commits
256 Forks
23 Watching
6 Branches
30 Contributors
Updated on 28 Nov 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-28.9%
172,960
Compared to previous day
Last week
-1%
1,046,163
Compared to previous week
Last month
7%
4,631,166
Compared to previous month
Last year
83.1%
41,824,242
Compared to previous year
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.
Need a job scheduler with support for worker threads and cron syntax? Try out the Bree job scheduler!
Install node-cron using npm:
1npm install --save node-cron
Import node-cron and schedule a task:
1const cron = require('node-cron'); 2 3cron.schedule('* * * * *', () => { 4 console.log('running a task every minute'); 5});
1import cron from 'node-cron'; 2 3cron.schedule('* * * * *', () => { 4 console.log('running a task every minute'); 5});
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) |
You may use multiples values separated by comma:
1import cron from 'node-cron'; 2 3cron.schedule('1,2,4,5 * * * *', () => { 4 console.log('running every minute 1, 2, 4 and 5'); 5});
You may also define a range of values:
1import cron from 'node-cron'; 2 3cron.schedule('1-5 * * * *', () => { 4 console.log('running every minute to 1 from 5'); 5});
Step values can be used in conjunction with ranges, following a range with '/' and a number. e.g: 1-10/2
that is the same as 2,4,6,8,10
. Steps are also permitted after an asterisk, so if you want to say “every two minutes”, just use */2
.
1import cron from 'node-cron'; 2 3cron.schedule('*/2 * * * *', () => { 4 console.log('running a task every two minutes'); 5});
For month and week day you also may use names or short names. e.g:
1import cron from 'node-cron'; 2 3cron.schedule('* * * January,September Sunday', () => { 4 console.log('running on Sundays of January and September'); 5});
Or with short names:
1import cron from 'node-cron'; 2 3cron.schedule('* * * Jan,Sep Sun', () => { 4 console.log('running on Sundays of January and September'); 5});
Schedules given task to be executed whenever the cron expression ticks.
Arguments:
string
: Cron expressionFunction
: Task to be executedObject
: Optional configuration for job scheduling.boolean
to set if the created task is scheduled. Default true
;boolean
to set if the created task should be able to recover missed executions. Default false
;Asia/Shanghai
, Asia/Kolkata
, America/Sao_Paulo
.Example:
1 import cron from 'node-cron'; 2 3 cron.schedule('0 1 * * *', () => { 4 console.log('Running a job at 01:00 at America/Sao_Paulo timezone'); 5 }, { 6 scheduled: true, 7 timezone: "America/Sao_Paulo" 8 });
Starts the scheduled task.
1import cron from 'node-cron'; 2 3const task = cron.schedule('* * * * *', () => { 4 console.log('stopped task'); 5}, { 6 scheduled: false 7}); 8 9task.start();
The task won't be executed unless re-started.
1import cron from 'node-cron'; 2 3const task = cron.schedule('* * * * *', () => { 4 console.log('will execute every minute until stopped'); 5}); 6 7task.stop();
Validate that the given string is a valid cron expression.
1import cron from 'node-cron'; 2 3const valid = cron.validate('59 * * * *'); 4const invalid = cron.validate('60 * * * *');
You can name your tasks to make it easier to identify them in the logs.
1import cron from 'node-cron'; 2 3const task = cron.schedule('* * * * *', () => { 4 console.log('will execute every minute until stopped'); 5}, { 6 name: 'my-task' 7});
You can list all the tasks that are currently running.
1import cron from 'node-cron'; 2 3const tasks = cron.getTasks(); 4 5for (let [key, value] of tasks.entries()) { 6 console.log("key", key) 7 console.log("value", value) 8}
value is an object with the following properties:
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.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 7/11 approved changesets -- score normalized to 6
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
13 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More