Coder Social home page Coder Social logo

node-cron's Introduction

Node Cron

npm npm Coverage Status Code Climate Build Status Dependency Status devDependency Status

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 = require('node-cron');

cron.schedule('* * * * *', function(){
  console.log('running a task every minute');
});

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 = require('node-cron');

cron.schedule('1,2,4,5 * * * *', function(){
  console.log('running every minute 1, 2, 4 and 5');
});

Using ranges

You may also define a range of values:

var cron = require('node-cron');

cron.schedule('1-5 * * * *', function(){
  console.log('running every minute to 1 from 5');
});

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 = require('node-cron');

cron.schedule('*/2 * * * *', function(){
  console.log('running a task every two minutes');
});

Using names

For month and week day you also may use names or short names. e.g:

var cron = require('node-cron');

cron.schedule('* * * January,September Sunday', function(){
  console.log('running on Sundays of January and September');
});

Or with short names:

var cron = require('node-cron');

cron.schedule('* * * Jan,Sep Sun', function(){
  console.log('running on Sundays of January and September');
});

Cron methods

Schedule

Schedules given task to be executed whenever the cron expression ticks.

Arguments:

  • !string expression - Cron expression
  • !Function func - Task to be executed
  • boolean? immediateStart - Whether to start scheduler immediately after create.

ScheduledTask methods

Start

Starts the scheduled task.

var cron = require('node-cron');

var task = cron.schedule('* * * * *', function() {
  console.log('immediately started');
}, false);

task.start();

Stop

The task won't be executed unless re-started.

var cron = require('node-cron');

var task = cron.schedule('* * * * *', function() {
  console.log('will execute every minute until stopped');
});

task.stop();

Destroy

The task will be stopped and completely destroyed.

var cron = require('node-cron');

var task = cron.schedule('* * * * *', function() {
  console.log('will not execute anymore, nor be able to restart');
});

task.destroy();

Issues

Feel free to submit issues and enhancement requests here.

Contributors

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.

License

node-cron is under ISC License.

node-cron's People

Contributors

merencia avatar recuencojones avatar freedomben avatar caarlos0 avatar waldyrious avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.