0.0.3 • Published 5 years ago

cron-pkjq v0.0.3

Weekly downloads
14
License
MIT
Repository
github
Last release
5 years ago

NPM

CRON

Scheduler with cron syntax

Installation

  • npm install cron-pkjq

Usage

In Node.js:

const cron = require("cron-pkjq");

let newYearCron = cron.add('@yearly', () => { console.log('Happy new year!!!'); });


// cron may be stopped
cron.stop(newYearCron);

// and may be started again
cron.start(newYearCron);

Syntax

This implementation uses cron-parser for parse cron-expressions. Supported syntax should be looked there. You may use crontab.guru to check your cron-expression.

Quick reference to supported cron syntax:

The format is a five(or six) time-and-date fields or a predefined macros with prefix '@'. Commands will be executed when the 'minute', 'hour', and 'month of the year' fields match the current time, and at least one of the two 'day' fields ('day of month', or 'day of week') match the current time. The day of a command's execution can be specified in the following two fields - 'day of month', and 'day of week'. If both fields are restricted (i.e., do not contain the "*" character), the command will be run when either field matches the current time.

fields format

  ┌─────────────── second (optional)
  │  ┌──────────── minute
  │  │ ┌────────── hour
  │  │ │ ┌──────── day of month
  │  │ │ │ ┌────── month
  │  │ │ │ │ ┌──── day of week
 [*] * * * * *

values

fieldvalue
second0-59
minute0-59
hour0-23
day of month1-31
month1-12 or short names
day of week0-7 (0 and 7 are sunday) or short names

A field may contain an asterisk (*), which always stands for "first-last".

multiples values and ranges

Ranges of numbers are allowed. Ranges are two numbers separated with a hyphen. The specified range is inclusive. For example, 8-11 for an 'hours' .

cron.schedule('* 8-10 * * *', () => { console.log('entry specifies execution at hours 8, 9, 10, and 11'); });

Lists are allowed. A list is a set of numbers (or ranges) separated by commas. Examples: "1,2,5,9", "0-4,8-12".

cron.schedule('1,2,4,5 * * * *', () => { console.log('entry specifies execution every minute 1, 2, 4, 5'); });

Step values can be used in conjunction with ranges. Following a range with "/" specifies skips of the number's value through the range. For example, "0-23/2" can be used in the 'hours' field to specify command execution for every other hour. Step values are also permitted after an asterisk, so if specifying a job to be run every two hours, you can use "*/2".

cron.schedule('*/2 * * * *', () => { console.log('every 2 minutes'); });

Names can also be used for the 'month' and 'day of week' fields. Use the first three letters of the particular day or month (case does not matter). But ranges or lists of names are not allowed.

macros

macrosequivalentcomment
@hourly'0 * * * *'
@daily'0 0 * * *'At 12:00am
@weekly'0 0 * * 0'At 12:00am on Sunday
@monthly'0 0 1 * *'1st of every month at 12:00am
@yearly'0 0 1 1 *'1st January every year at 12:00am

short names of month

month
1jan
2feb
3mar
4apr
5may
6jun
7jul
8aug
9sep
10oct
11nov
12dec

short names of day of weak

day of weak
0sun
1mon
2tue
3wed
4thu
5fri
6sat
7sun

About copyrights read legal_notice, please!