How can I programmatically shutdown a node program and restart it without using any libraries?

偶尔善良 提交于 2019-12-08 02:55:55

问题


I need to shutdown a node program and have it restart. I need to do this within the itself program without having to use something like forever needing to be setup.

I know I can use process.exit() to shut the program down, but anything I can think of that would open it back up that I can kick off from within node would be killed by process.exit() before it could finish. Is there a way I'm not seeing to detach a exec call from the process before I exit? Any other ideas? Do I have to suck it up and go use forever?


回答1:


Quick and dirty, not very well tested:

var child_process = require('child_process');

process.on('SIGINT', function() {
  console.log('restarting...');
  child_process.fork(__filename); // TODO: pass args...
  process.exit(0);
});

console.log('Running as %d', process.pid);

setTimeout(function(){}, 1000000); // just some code to keep the process running


来源:https://stackoverflow.com/questions/19517789/how-can-i-programmatically-shutdown-a-node-program-and-restart-it-without-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!