DrakaSAN
DrakaSAN

Reputation: 7863

Gracefully restart in node.js

I have a internet half-dependent code in node.js

It depend of internet on update and start only.

With process.on('uncaughtException') I am able to get all internet connexion problem.

What I would like is a way to stop the script, wait a given amount of time, and restart the app from the beginning.

I have tried:

process.on('uncaughtException', function(err) {
    console.log('Caught fatal error ' + err + ', will restart in 10sec');
    setTimeout(function() {
        var arg = ['-c', 'sh start.sh'],
            child = spawn('bash', arg);
        process.exit();
    }, 10000);
});

It does restart the script, but in another instance, which mean when I ctrl + c to kill the process, my log show that X instance of node.js where running. (Log end by multiple 'User killed process. Killing path/to/app.js') I would like to kill the script before spawning start.sh

(nb: start.sh is a simple bash script which start my app with some parameters).

Upvotes: 1

Views: 361

Answers (1)

Guy Gavriely
Guy Gavriely

Reputation: 11396

have an external tool to monitor it and start it back after it exit, you can use forever, node-supervisor or a simple recurrent cron for that.

Upvotes: 3

Related Questions