javascriptnode.jsapplication-restart

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


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?


Solution

  • 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