3

I have 8 javascripts which I start via node on my linux machine.

what I want is when a script is finished and closed itself it shall automatically restart.

Therefore I created a start.sh which looks like this

while true; do
node 1.js & node 2.js & node 3.js & node 4.js & node 5.js & node 6.js & node 7.js & node 8.js;
done

When i start it via ./start.sh alle the scripts start, but they dont automatically restart once they're finished.

Is there any thing i can add to the script itself that it restarts? I tried

while(true) {
//code//
}

but that restarts the script even if its not finished.

Gaurav Dave
  • 6,838
  • 9
  • 25
  • 39
  • If you can edit the JS files, you should just wrap the entire thing in `while (1)`, otherwise take a look at the `watch` command on Linux. – wilsonzlin Apr 27 '16 at 10:54

2 Answers2

0

Try this using async:

  var async = require('async');

  var onejs = require('1.js');
  var twojs = require('2.js');
  var threejs = require('3.js');



  // a simple recursive function that gets called once all functions
  //are done executing, this should go on forever
 function start() {
      async.parallel([
            onejs, 
            twojs, 
            threejs
      ], function done() {
            start();
      });
 }

  // remember your scripts should call async's callback, 
  // i am assuming you know you know how to use asyncjs.
AJS
  • 1,993
  • 16
  • 26
0

Your shell snippet has a few problems.

To begin with, every of your node script.js calls are followed by a & to execute them in background, except the last one.
In the current setting, you will start running each script, and wait for the 8th one to finish. At this point you will loop and start each script again, regardless of whether their last execution finished.

While other node.js based answers are clearly superior, here's what I think you could have done with a shell script :

while true; do node 1.js; done &
while true; do node 2.js; done &
[...]
while true; do node 8.js; done &

This launches 8 loops in the background, so you don't have to wait for the 1st loop to complete to start the next. However, each loop waits for its script to finish before it loops.
That way, each script launch is independent from the other's but depend on their previous execution.

Aaron
  • 24,009
  • 2
  • 33
  • 57