UNPKG

5.32 kBMarkdownView Raw
1# Node Cron
2
3[![npm](https://img.shields.io/npm/l/node-cron.svg)](https://github.com/merencia/node-cron/blob/master/LICENSE.md)
4[![npm](https://img.shields.io/npm/v/node-cron.svg)](https://img.shields.io/npm/v/node-cron.svg)
5[![Coverage Status](https://coveralls.io/repos/github/merencia/node-cron/badge.svg?branch=master)](https://coveralls.io/github/merencia/node-cron?branch=master)
6[![Code Climate](https://codeclimate.com/github/merencia/node-cron/badges/gpa.svg)](https://codeclimate.com/github/merencia/node-cron)
7[![Build Status](https://travis-ci.org/merencia/node-cron.svg?branch=master)](https://travis-ci.org/merencia/node-cron)
8[![Dependency Status](https://david-dm.org/merencia/node-cron.svg)](https://david-dm.org/merencia/node-cron)
9[![devDependency Status](https://david-dm.org/merencia/node-cron/dev-status.svg)](https://david-dm.org/merencia/node-cron#info=devDependencies)
10
11The node-cron module is tiny task scheduler in pure JavaScrip for node.js based on [GNU crontab](https://www.gnu.org/software/mcron/manual/html_node/Crontab-file.html). This module allows you to schedule task in node.js using full crontab syntax.
12
13
14## Getting Started
15
16Install node-cron using npm:
17
18```console
19$ npm install --save node-cron
20```
21
22Import node-cron and schedule a task:
23
24```javascript
25var cron = require('node-cron');
26
27cron.schedule('* * * * *', function(){
28 console.log('running a task every minute');
29});
30```
31
32## Cron Syntax
33
34This is a quick reference to cron syntax and also shows the options supported by node-cron.
35
36### Allowed fields
37
38```
39 # ┌────────────── second (optional)
40 # │ ┌──────────── minute
41 # │ │ ┌────────── hour
42 # │ │ │ ┌──────── day of month
43 # │ │ │ │ ┌────── month
44 # │ │ │ │ │ ┌──── day of week
45 # │ │ │ │ │ │
46 # │ │ │ │ │ │
47 # * * * * * *
48```
49
50### Allowed values
51
52| field | value |
53|--------------|---------------------|
54| second | 0-59 |
55| minute | 0-59 |
56| hour | 0-23 |
57| day of month | 1-31 |
58| month | 1-12 (or names) |
59| day of week | 0-7 (or names, 0 or 7 are sunday) |
60
61
62#### Using multiples values
63
64You may use multiples values separated by comma:
65
66```javascript
67var cron = require('node-cron');
68
69cron.schedule('1,2,4,5 * * * *', function(){
70 console.log('running every minute 1, 2, 4 and 5');
71});
72```
73
74#### Using ranges
75
76You may also define a range of values:
77
78```javascript
79var cron = require('node-cron');
80
81cron.schedule('1-5 * * * *', function(){
82 console.log('running every minute to 1 from 5');
83});
84```
85
86#### Using step values
87
88Step 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`.
89
90```javascript
91var cron = require('node-cron');
92
93cron.schedule('*/2 * * * *', function(){
94 console.log('running a task every two minutes');
95});
96```
97
98#### Using names
99
100For month and week day you also may use names or short names. e.g:
101
102```javascript
103var cron = require('node-cron');
104
105cron.schedule('* * * January,September Sunday', function(){
106 console.log('running on Sundays of January and September');
107});
108```
109
110Or with short names:
111
112```javascript
113var cron = require('node-cron');
114
115cron.schedule('* * * Jan,Sep Sun', function(){
116 console.log('running on Sundays of January and September');
117});
118```
119
120## Cron methods
121
122### Schedule
123
124Schedules given task to be executed whenever the cron expression ticks.
125
126Arguments:
127
128- !string expression - Cron expression
129- !Function func - Task to be executed
130- boolean? immediateStart - Whether to start scheduler immediately after create.
131
132## ScheduledTask methods
133
134### Start
135
136Starts the scheduled task.
137
138```javascript
139var cron = require('node-cron');
140
141var task = cron.schedule('* * * * *', function() {
142 console.log('immediately started');
143}, false);
144
145task.start();
146```
147
148### Stop
149
150The task won't be executed unless re-started.
151
152```javascript
153var cron = require('node-cron');
154
155var task = cron.schedule('* * * * *', function() {
156 console.log('will execute every minute until stopped');
157});
158
159task.stop();
160```
161
162### Destroy
163
164The task will be stopped and completely destroyed.
165
166```javascript
167var cron = require('node-cron');
168
169var task = cron.schedule('* * * * *', function() {
170 console.log('will not execute anymore, nor be able to restart');
171});
172
173task.destroy();
174```
175
176## Issues
177
178Feel free to submit issues and enhancement requests [here](https://github.com/merencia/node-cron/issues).
179
180## Contributors
181
182In general, we follow the "fork-and-pull" Git workflow.
183
184 - Fork the repo on GitHub;
185 - Commit changes to a branch in your fork;
186 - Pull request "upstream" with your changes;
187
188NOTE: Be sure to merge the latest from "upstream" before making a pull request!
189
190Please 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.
191
192## License
193
194node-cron is under [ISC License](https://github.com/merencia/node-cron/blob/master/LICENSE.md).