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.
Getting Started
Install node-cron using npm:
$ npm install --save node-cron
Import node-cron and schedule a task:
var cron = ; cron;
Cron Syntax
This is a quick reference to cron syntax and also shows the options supported by node-cron.
Allowed fields
# ┌────────────── second (optional)
# │ ┌──────────── minute
# │ │ ┌────────── hour
# │ │ │ ┌──────── day of month
# │ │ │ │ ┌────── month
# │ │ │ │ │ ┌──── day of week
# │ │ │ │ │ │
# │ │ │ │ │ │
# * * * * * *
Allowed values
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) |
Using multiples values
You may use multiples values separated by comma:
var cron = ; cron;
Using ranges
You may also define a range of values:
var cron = ; cron;
Using step values
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
.
var cron = ; cron;
Using names
For month and week day you also may use names or short names. e.g:
var cron = ; cron;
Or with short names:
var cron = ; cron;
Cron methods
Schedule
Schedules given task to be executed whenever the cron expression ticks.
Arguments:
- expression
string
: Cron expression - function
Function
: Task to be executed - options
Object
: Optional configuration for job scheduling.
Options
- scheduled: A
boolean
to set if the created task is scheduled. Defaulttrue
; - timezone: The timezone that is used for job scheduling;
Example:
var cron = ; cron;
ScheduledTask methods
Start
Starts the scheduled task.
var cron = ; var task = cron; taskstart;
Stop
The task won't be executed unless re-started.
var cron = ; var task = cron; task;
Destroy
The task will be stopped and completely destroyed.
var cron = ; var task = cron; task;
Validate
Validate that the given string is a valid cron expression.
var cron = ; var valid = cron;var invalid = cron;
Issues
Feel free to submit issues and enhancement requests here.
Contributing
In general, we follow the "fork-and-pull" Git workflow.
- Fork the repo on GitHub;
- Commit changes to a branch in your fork;
- Pull request "upstream" with your changes;
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.
Contributors
This project exists thanks to all the people who contribute.
Backers
Thank you to all our backers! 🙏 [Become a backer]
Sponsors
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]
License
node-cron is under ISC License.