node.jsnode-schedule

How to schedule the node js script to run automatically on windows?


This script

0 0/3 * * * node test.js

is used to schedule the job in Ubuntu, How to set the same way in Windows using node-schedule npm package?

As a work around , have scheduled the script in Task Scheduler:

cmd /c c:\node\node.exe c:\myscript.js

I want to to know how this can be done in node-schedule npm package.


Solution

  • From https://npmjs.org/package/node-schedule:

    Execute a cron job every 5 Minutes = */5 * * * *

    So (according to the NodeJS docs) you can use the child_process npm module to run the script.

    Like this:

    const { spawn } = require('child_process');
    const schedule = require('node-schedule');
    
    schedule.scheduleJob('*/5 * * * *', function() {
      spawn('node', ['test.js']);
    });