UNPKG

6.03 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 JavaScript 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[![NPM](https://nodei.co/npm/node-cron.png?downloads=true&downloadRank=true&stars=false)](https://nodei.co/npm/node-cron/)
14
15
16## Getting Started
17
18Install node-cron using npm:
19
20```console
21$ npm install --save node-cron
22```
23
24Import node-cron and schedule a task:
25
26```javascript
27var cron = require('node-cron');
28
29cron.schedule('* * * * *', () => {
30 console.log('running a task every minute');
31});
32```
33
34## Cron Syntax
35
36This is a quick reference to cron syntax and also shows the options supported by node-cron.
37
38### Allowed fields
39
40```
41 # ┌────────────── second (optional)
42 # │ ┌──────────── minute
43 # │ │ ┌────────── hour
44 # │ │ │ ┌──────── day of month
45 # │ │ │ │ ┌────── month
46 # │ │ │ │ │ ┌──── day of week
47 # │ │ │ │ │ │
48 # │ │ │ │ │ │
49 # * * * * * *
50```
51
52### Allowed values
53
54| field | value |
55|--------------|---------------------|
56| second | 0-59 |
57| minute | 0-59 |
58| hour | 0-23 |
59| day of month | 1-31 |
60| month | 1-12 (or names) |
61| day of week | 0-7 (or names, 0 or 7 are sunday) |
62
63
64#### Using multiples values
65
66You may use multiples values separated by comma:
67
68```javascript
69var cron = require('node-cron');
70
71cron.schedule('1,2,4,5 * * * *', () => {
72 console.log('running every minute 1, 2, 4 and 5');
73});
74```
75
76#### Using ranges
77
78You may also define a range of values:
79
80```javascript
81var cron = require('node-cron');
82
83cron.schedule('1-5 * * * *', () => {
84 console.log('running every minute to 1 from 5');
85});
86```
87
88#### Using step values
89
90Step 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`.
91
92```javascript
93var cron = require('node-cron');
94
95cron.schedule('*/2 * * * *', () => {
96 console.log('running a task every two minutes');
97});
98```
99
100#### Using names
101
102For month and week day you also may use names or short names. e.g:
103
104```javascript
105var cron = require('node-cron');
106
107cron.schedule('* * * January,September Sunday', () => {
108 console.log('running on Sundays of January and September');
109});
110```
111
112Or with short names:
113
114```javascript
115var cron = require('node-cron');
116
117cron.schedule('* * * Jan,Sep Sun', () => {
118 console.log('running on Sundays of January and September');
119});
120```
121
122## Cron methods
123
124### Schedule
125
126Schedules given task to be executed whenever the cron expression ticks.
127
128Arguments:
129
130- **expression** `string`: Cron expression
131- **function** `Function`: Task to be executed
132- **options** `Object`: Optional configuration for job scheduling.
133
134#### Options
135
136 - **scheduled**: A `boolean` to set if the created task is schaduled. Default `true`;
137 - **timezone**: The timezone that is used for job scheduling;
138
139 **Example**:
140
141 ```js
142 var cron = require('node-cron');
143
144 cron.schedule('0 1 * * *', () => {
145 console.log('Runing a job at 01:00 at America/Sao_Paulo timezone');
146 }, {
147 scheduled: true,
148 timezone: "America/Sao_Paulo"
149 });
150 ```
151
152## ScheduledTask methods
153
154### Start
155
156Starts the scheduled task.
157
158```javascript
159var cron = require('node-cron');
160
161var task = cron.schedule('* * * * *', () => {
162 console.log('stoped task');
163}, {
164 scheduled: false
165});
166
167task.start();
168```
169
170### Stop
171
172The task won't be executed unless re-started.
173
174```javascript
175var cron = require('node-cron');
176
177var task = cron.schedule('* * * * *', () => {
178 console.log('will execute every minute until stopped');
179});
180
181task.stop();
182```
183
184### Destroy
185
186The task will be stopped and completely destroyed.
187
188```javascript
189var cron = require('node-cron');
190
191var task = cron.schedule('* * * * *', () => {
192 console.log('will not execute anymore, nor be able to restart');
193});
194
195task.destroy();
196```
197
198### Validate
199
200Validate that the given string is a valid cron expression.
201
202```javascript
203var cron = require('node-cron');
204
205var valid = cron.validate('59 * * * *');
206var invalid = cron.validate('60 * * * *');
207```
208
209## Issues
210
211Feel free to submit issues and enhancement requests [here](https://github.com/merencia/node-cron/issues).
212
213## Contributors
214
215In general, we follow the "fork-and-pull" Git workflow.
216
217 - Fork the repo on GitHub;
218 - Commit changes to a branch in your fork;
219 - Pull request "upstream" with your changes;
220
221NOTE: Be sure to merge the latest from "upstream" before making a pull request!
222
223Please 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.
224
225## License
226
227node-cron is under [ISC License](https://github.com/merencia/node-cron/blob/master/LICENSE.md).