node-schedule should run every 1 hour in node js

1.9k Views Asked by At

I have created crone job for every minute

import * as schedule from "node-schedule"

schedule.scheduleJob('* * * * *', async () => {
            console.log("running every minute")
        });

the above working perfectly for every minute. similarly I have task which has to execute for every hour. I tried like below

schedule.scheduleJob('0 0 */1 * *', async () => {
            console.log("running every minute")
        });

But unfortunately its not working every hour as expected. Is there any thing I'm missing

4

There are 4 best solutions below

0
On

based on node-schedule the third * is hour

*    *    *    *    *    *
┬    ┬    ┬    ┬    ┬    ┬
│    │    │    │    │    │
│    │    │    │    │    └ 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)

for every one hour just try

schedule.scheduleJob('0 0 */1 * * *', async () => {
        console.log("running every minute")
 });
0
On

For every Hour it should be :

schedule.scheduleJob('0 0 */1 * * *', async () => {
     console.log("running every hour")
 });
0
On

i think you want "0 */1 * * *". Also you can use this great website to make sure your crontab is correct: https://crontab.guru/

1
On

You should wirte the cron string completely with length of 6 like "0 0 */1 * * *", because it is parsed backwards by schedule.