简体   繁体   中英

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).

有一个外部工具来监视它并在它退出后重新启动它,你可以永远使用, node-supervisor或一个简单的recurrent cron

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM