2

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?

Brian
  • 3,264
  • 4
  • 30
  • 43
  • Possible duplicate: http://stackoverflow.com/questions/9357757/node-js-app-that-can-restart-itself. Is there a particular reason you don't want to use forever? – hopper Oct 22 '13 at 12:42
  • Possible duplicate of [node.js app that can restart itself](https://stackoverflow.com/questions/9357757/node-js-app-that-can-restart-itself) – iono Mar 27 '19 at 07:11
  • I'm back 6 years later because this got marked as a duplicate. The linked question shows solutions up until 2017 that required other modules. When this question that asked for a solution to this problem without another module. As for why I didn't want to install forever, this was on an industrial embedded application that had to be deployed on multiple machines. I had limited repository space and didn't want to have to bring in another library and it's dependencies and capture them to keep subsequent changes from breaking my solution or necessitating quality testing to verify again. – Brian Mar 28 '19 at 16:24

1 Answers1

2

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
robertklep
  • 198,204
  • 35
  • 394
  • 381
  • This worked like a charm. Thanks so much! I got the way the node process was started from process.argv and just ran it again. – Brian Oct 23 '13 at 11:18