0

I need to run cron once at a particular tine and after that have to stop the job

const cron = require("node-cron");
const SendEmail = (payload) => {
  console.log("SENDING EMAIL", payload);
};
const ScheduleCronJob = (payload, cronTime) => {
  console.log("ScheduleCronJob.........", cronTime);
  const job = cron.schedule(cronTime, () => {
  console.log("You will see this message every second");
  SendEmail(payload);
  job.stop();
});
  return job;
};
const payload = { name: "sarath" };
const job = ScheduleCronJob(payload, "05 17 18 2 5");
job.start();

I am starting and stopping the cron job this is not working fine for me. if i replace with "* * * * *" then it is working fine. can anyone explain wher i did wrong

Dudi Boy
  • 4,551
  • 1
  • 15
  • 30
Nettemsarath
  • 91
  • 1
  • 5

1 Answers1

0

Assuming your cron schedule 05 17 18 2 5 is set to:

 At 17:05 on day-of-month 18 and on Friday in February.

Suggesting to test your cron schedule in here.

Suggesting to pay attention to timezone calculation in node-cron

Stopping the current job is not related to future process that started by contab at some time in the future.

You need to use pkill command or killall command with full path to the process command or the pid. Suggesting to study node-cron documentation about pid and process names.

Dudi Boy
  • 4,551
  • 1
  • 15
  • 30