Akshay Kashid
Akshay Kashid

Reputation: 89

How to schedule node-cron job every new month?

I am using node-cron package for scheduling node-cron jobs. I want to schedule an node-cron job which will run every new month. for example: My node-cron job should run at 1 September 2020 after that it should run at 1 October 2020 and and so on..! Please help me out for the above issue. Thanks in advance.

Upvotes: 8

Views: 10507

Answers (3)

Reda El Ouahabi
Reda El Ouahabi

Reputation: 91

cron.schedule("* * * 1 * *", function() { // Do something });

OR

cron.schedule("* * 1 * *", function() { // Do something });

NOTE : remember that the first * of the six * is optional

Upvotes: 0

Giovanni Pizzato
Giovanni Pizzato

Reputation: 141

I've tested accepted answer's code and noticed that there is something off.

cron.schedule(* * 1 * *) will make code run every first day of the month, and every hour and every minute. This means if it is first day of month, code will run once every minute.

To correct this issue (and actually run once a month, not multiple times in one day) we change: cron.schedule(* * 1 * *) to: cron.schedule(0 0 1 * *) so code runs every first day, at 00:00.

Upvotes: 14

Getter Jetter
Getter Jetter

Reputation: 2081

Following this tutorial I believe you just have to do:

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

cron.schedule("* * 1 * *", function() {
  // Do something
});

where:

enter image description here

Upvotes: 2

Related Questions