3

I have an node app that runs as a cron job every few seconds:

   var CronJob = require('cron').CronJob;
   new CronJob('*/5 * * * * *', function(){
       console.log('Here invoke a script called requestdata');
   }, null, true, "America/Los_Angeles");

and I just want to call a script without invoking functions on it. So its not

          requestdata.foo();

but just call requestdata in same directory. how is this done? If it was on command line I would just do:

     node requestdata

but how do I do this inside another script?

5
  • Why not actually use Cron for this? And, you can always execute a child process... Commented Feb 16, 2015 at 2:50
  • Well I am planning to use cron for it but the bulk of the code is inside a file called requestdata.js so I am trying to find out how to call that stuff from within cron function without inline everything into the cron function. Commented Feb 16, 2015 at 2:51
  • I don't understand why the location of your script determines what system you should use for executing processes regularly. Commented Feb 16, 2015 at 2:52
  • Well lets assume I cannot modify requestdata.js, and make it into a function. I just want to know the correct way to invoke it from within the cron function. Commented Feb 16, 2015 at 2:53
  • Why not just use a child process then? You could also use the VM, load the file and exec(), simply require() it, etc. There's plenty you could do, but what you do depends on your specific needs. Commented Feb 16, 2015 at 2:54

2 Answers 2

5

Use child_process, like so

var cp = require('child_process');
cp.fork(__dirname + '/request data.js');

See http://nodejs.org/api/child_process.html

Sign up to request clarification or add additional context in comments.

1 Comment

how to pass parameter to that '/request data.js' script
2

This solution also shows how to pass parameters to the "request data.js" script

var cp = require('child_process');
cp.fork(__dirname + '/request data.js',[array,of,string,prams]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.