Node-Schedule trigger function each 2 hours

I am using the package node-schedule to avoid using setInterval in my application, I need to trigger 2 functions, one each 30 minuts and one each 2 hours, on the node-schedule docs I can see :

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

So for my function who needed to be triggered each 30min I done this :
*/30 * * * * and this is working, the function is triggered each 30min.

For my second function (who need to be triggered each 2 hours) I used this: * */2 * * * and thats not working at all unfortunately (it seems like it trigger each 10 mins)..

If someone could help me thanks in advance !

Here is my code if necessary :

import { scheduleJob } from 'node-schedule';

(async function() {
    const firstScheduler = scheduleJob('* */2 * * *', async function() {
        // Not Working
    });
    const secScheduler = scheduleJob('*/30 * * * *', async function() {
        // Working
    });
})();

>Solution :

* means ‘every’. Your current expression means, every minute of every 2 hours (12 am, 2 am, 4 am, …).

Adding 0 in the minute section will resolve it. It executes exactly at 0th minute of every 2 hours.

const firstScheduler = scheduleJob('0 */2 * * *', async function() {
     // Run every 2 hours
});

Leave a Reply