M.Uma
M.Uma

Reputation: 105

scheduling file every 1 hour using nodejs

I want to run a print statement every 1 hour in Windows environment using node.js. I use the package node-schedule.

But when I tried running this, the output is not as expected. I think my schedule format is wrong.

So I tried the following code:

var schedule = require('node-schedule');

var j = schedule.scheduleJob('*/1 * * *', function(){
    console.log('The answer to life, the universe, and everything!');
});

Upvotes: 10

Views: 28312

Answers (8)

8exor
8exor

Reputation: 1

it will run every hour at 0 min, 0 secs.

const job = schedule.scheduleJob('0 0 */1 * * *', function(fireDate){
  console.log('This job was supposed to run at ' + fireDate + ', but actually ran at ' + new Date());
});

Upvotes: 0

Tho
Tho

Reputation: 25050

You can try this:

const schedule = require('node-schedule');
const job = schedule.scheduleJob('1 * * * *', () => { // run every hour at minute 1
    console.log('The answer to life, the universe, and everything!');
});

More examples:

  • Every 2 minutes: */2 * * * *
  • Every 2 hours when at 5 minute mark: 5 */2 * * *

You should use this friendly tool to read and verify the config. Example:

enter image description here

It's worth noting that not all cronjob libs support the same fine-grain time level. For example:

  • Linux crontab: at the minute level (allow 5 * in the config)
  • node-schedule: at the second level (allow 6 * in the config)

Upvotes: 5

Kari
Kari

Reputation: 1360

This runs when "second is zero" and "minute is zero", and "any hour", "any day", "any month", "any day-of-week". Basically, every hour.

schedule.scheduleJob("0 0 * * * *", function() {
   console.log('I run every hour');
}

Upvotes: 0

Carlotta
Carlotta

Reputation: 334

You need to add one more "*" to the cron-style. They do have a good README.md if you want to find out more about it.

const schedule = require('node-schedule');

const job = schedule.scheduleJob('* */1 * * *', function(){
  console.log('The answer to life, the universe, and everything!');
});

The cron format consists of:

*    *    *    *    *    *
┬    ┬    ┬    ┬    ┬    ┬
│    │    │    │    │    |
│    │    │    │    │    └ day of week (0 - 7) (0 or 7 is Sun)
│    │    │    │    └───── month (1 - 12)
│    │    │    └────────── day of month (1 - 31)
│    │    └─────────────── hour (0 - 23)
│    └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, OPTIONAL)

EDIT

If you would like to run it once every hour on a specific minute, you can also do this and switch the 0 with any minute 0-59:

const schedule = require('node-schedule');

const job = schedule.scheduleJob('0 * * * *', function(){
  console.log('The answer to life, the universe, and everything!');
});

Upvotes: 0

Raja Rajan
Raja Rajan

Reputation: 91

This will run every 1 hour, 0th minute, 0th second( ex: execute at 1PM, 2PM, 3PM etc):

var CronJob = require('cron').CronJob;
var job = new CronJob('0 0 */1 * * *', function() {
  console.log('The answer to life, the universe, and everything!');
});
job.start();

Refer: https://www.npmjs.com/package/cron

Upvotes: 1

BlackMamba
BlackMamba

Reputation: 10254

var schedule = require('node-schedule');
var j = schedule.scheduleJob('* 1 * * *', function(){  // this for one hour
console.log('The answer to life, the universe, and everything!');
});

The cron format consists of:

*    *    *    *    *    *
┬    ┬    ┬    ┬    ┬    ┬
│    │    │    │    │    |
│    │    │    │    │    └ day of week (0 - 7) (0 or 7 is Sun)
│    │    │    │    └───── month (1 - 12)
│    │    │    └────────── day of month (1 - 31)
│    │    └─────────────── hour (0 - 23)
│    └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, OPTIONAL)

Here is the link: https://github.com/node-schedule/node-schedule

Upvotes: 11

Ramesh Node
Ramesh Node

Reputation: 1

This will run by based on your time zone, for example, Now the time in your time is 12:30, your scheduler will run by 01:00.Please check

Upvotes: 0

mestarted
mestarted

Reputation: 413

Why can't you use setInerval()

setInterval(function () {
   console.log('The answer to life, the universe, and everything!');
}, 1 * 60 * 60 * 1000); // 1 hour

Upvotes: 4

Related Questions