Socket
Socket
Sign inDemoInstall

cron

Package Overview
Dependencies
0
Maintainers
1
Versions
66
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    cron

CronJob's for your node


Version published
Maintainers
1
Install size
43.5 kB
Created

Package description

What is cron?

The cron npm package is used for scheduling tasks to be executed at specific times or intervals. It is inspired by the Unix cron scheduler and allows for the use of cron syntax to schedule tasks in a Node.js environment. This package is useful for setting up jobs like backups, sending emails, or cleaning up databases at regular intervals.

What are cron's main functionalities?

Basic Cron Job

This feature allows you to create a basic cron job that runs at a specified interval. In the provided code sample, a new CronJob is created that logs a message to the console every second.

"const CronJob = require('cron').CronJob;\nconst job = new CronJob('* * * * * *', function() {\n  console.log('You will see this message every second');\n}, null, true, 'America/Los_Angeles');\njob.start();"

Time Zone Support

This feature demonstrates the ability to specify a time zone for the cron job. The code sample schedules a job to run at 11:30 AM, according to the 'America/New_York' time zone, from Monday to Friday.

"const CronJob = require('cron').CronJob;\nconst job = new CronJob('00 30 11 * * 1-5', function() {\n  console.log('This runs at 11:30 AM (server time) every Monday through Friday.');\n}, null, true, 'America/New_York');\njob.start();"

Dynamic Job Scheduling

This feature allows for dynamic scheduling of jobs. The schedule can be updated or changed based on certain conditions or inputs. In the example, the 'dynamicSchedule' variable can be updated to change the job's schedule.

"const CronJob = require('cron').CronJob;\nlet dynamicSchedule = '00 30 11 * * 1-5'; // This can be dynamically changed\nconst job = new CronJob(dynamicSchedule, function() {\n  console.log('This job's schedule can be dynamically changed.');\n}, null, true, 'America/Los_Angeles');\njob.start();"

Other packages similar to cron

Readme

Source

node-cron

Build Status

Originally this project was a NodeJS fork of James Padolsey's cron.js.

After Craig Condon made some updates and changes to the code base this has evolved to something that has a bit of both. The cron syntax parsing is mostly James' while using timeout instead of interval is Craig's.

Additionally, this library goes beyond the basic cron syntax and allows you to supply a Date object. This will be used as the trigger for your callback. Cron syntax is still an acceptable CronTime format.

Usage (basic cron usage):

var cronJob = require('cron').CronJob;
new cronJob('* * * * * *', function(){
    console.log('You will see this message every second');
}, null, true, "America/Los_Angeles");

Available Cron patterns:

Asterisk. E.g. *
Ranges. E.g. 1-3,5
Steps. E.g. */2

Read up on cron patterns here.

Another cron example

var cronJob = require('cron').CronJob;
var job = new cronJob('00 30 11 * * 2-6', function(){
    // Runs every weekday (Monday through Friday)
    // at 11:30:00 AM. It does not run on Saturday
    // or Sunday.
  }, function () {
    // This function is executed when the job stops
  }, 
  true /* Start the job right now */,
  timeZone /* Time zone of this job. */
);

Another example with Date

var cronJob = require('cron').CronJob;
var job = new cronJob(new Date(), function(){
    //runs once at the specified date.
  }, function () {
    // This function is executed when the job stops
  }, 
  true /* Start the job right now */,
  timeZone /* Time zone of this job. */
);

For good measure

var cronJob = require('cron').CronJob;
var job = new cronJob({
  cronTime: '00 30 11 * * 2-6',
  onTick: function() {
    // Runs every weekday (Monday through Friday)
    // at 11:30:00 AM. It does not run on Saturday
    // or Sunday.
  },
  start: false,
  timeZone: "America/Los_Angeles"
});
job.start();

How to check if a cron pattern is valid:

	try {
		new cronJob('invalid cron pattern', function() {
			console.log('this should not be printed');
		})
	} catch(ex) {
		console.log("cron pattern not valid");
	}

Install

From source: `npm install`
From npm: `npm install cron`

If you want to specify timezones, you'll need to install the time module or place an entry for it in your package.json file.

`npm install time`

API

Parameter Based

CronJob

  • constructor(cronTime, onTick, onComplete, start) - Of note, the first parameter here can be a JSON object that has the below names and associated types (see examples above).
    • cronTime - [REQUIRED] - The time to fire off your job. This can be in the form of cron syntax or a JS Date object.
    • onTick - [REQUIRED] - The function to fire at the specified time.
    • onComplete - [OPTIONAL] - A function that will fire when the job is complete, when it is stopped.
    • start - [OPTIONAL] - Specifies whether to start the job after just before exiting the constructor.
  • start - Runs your job.
  • stop - Stops your job.

CronTime

  • constructor(time)
    • time - [REQUIRED] - The time to fire off your job. This can be in the form of cron syntax or a JS Date object.

Contributors

License

MIT

FAQs

Last updated on 25 Apr 2012

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc