Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup node-schedule for every day at 12am

I am using node-schedule to schedule my tasks. Now I need to schedule a job everyday at 12am. Below given is the code I am using,

var rule3 = schedule.scheduleJob('00 00 00 * * *', function(){
    console.log('my test job!');
});

This is not working for me.

Any help appreciated. Thanks in advance.

like image 623
Anna Avatar asked Oct 27 '15 11:10

Anna


People also ask

How do I create a Nodejs scheduler?

Scheduling a Simple Task with node-cron js file to create our simple task scheduler: const cron = require("node-cron"); const express = require("express"); const app = express(); cron. schedule("*/15 * * * * *", function () { console. log("---------------------"); console.

How does node schedule work?

Node Schedule is a flexible cron-like and not-cron-like job scheduler for Node. js. It allows you to schedule jobs (arbitrary functions) for execution at specific dates, with optional recurrence rules. It only uses a single timer at any given time (rather than reevaluating upcoming jobs every second/minute).

What is cron job in node?

Node-cron is an npm package written for node in pure JavaScript, and it is based on GNU crontab syntax. Therefore our first step is based on the learning node-cron and cron-syntaxes.

What is in cron job?

What Is a Cron Job? cron is a Linux utility that schedules a command or script on your server to run automatically at a specified time and date. A cron job is the scheduled task itself. Cron jobs can be very useful to automate repetitive tasks.


2 Answers

For anyone who is stuck on this also check what time your app is operating in. Your app by default might be in Greenwich Mean Time which would definitely make you think your schedule is not working. Toss a console.log(Date.now()) in a convenient location and check. You might just have to adjust the time on your schedule by a couple hours.

like image 115
Abe Schroeder Avatar answered Sep 23 '22 21:09

Abe Schroeder


You can use node-cron module simply.

var CronJob = require('cron').CronJob;
var job = new CronJob('00 00 12 * * 0-6', function() {
  /*
   * Runs every day
   * at 12:00:00 AM.
   */
  }, function () {
    /* This function is executed when the job stops */
  },
  true, /* Start the job right now */
  timeZone /* Time zone of this job. */
);

Read docs for more pattern.

like image 40
Furkan Başaran Avatar answered Sep 19 '22 21:09

Furkan Başaran