12

I've an app which initially creates static config files (once) and after files were written I need to reinitialize/restart the application. Is there something to restart a node.js app from itself?

This is required cause I've an application running in two runlevels in node.js. The initial one starts completly synchronus and after this level has been completed app is in async runlevel in a previously started environment.

I know there are tools like nodemon but that's not what I need in my case.

I tried to kill the app via process.kill() which is working but I can't listen to the kill event:

 // Add the listener
 process.on('exit', function(code) {
    console.log('About to exit with code:', code);
    // Start app again but how?
 });

 // Kill application
 process.kill();

Or is there a better, cleaner way to handle this?

Bernhard
  • 4,855
  • 5
  • 39
  • 70
  • 1
    If I got your problem correctly, I think [nodmon](http://nodemon.io/) will help you. – Mritunjay Jul 10 '14 at 02:50
  • [Here](http://strongloop.com/strongblog/comparison-tools-to-automate-restarting-node-js-server-after-code-changes-forever-nodemon-nodesupervisor-nodedev/) is a list of tools which will help you I think. – Mritunjay Jul 10 '14 at 02:51
  • @Mritunjay wrote nodemon is not what I need here cause the restart is not required for changes on development files – Bernhard Jul 10 '14 at 02:54
  • ohh sorry, then I think these won't be helpful for you – Mritunjay Jul 10 '14 at 02:57
  • I have a suggestion which will handle your issue but I don't know whether you want things to be done this way. Try using forever : https://www.npmjs.org/package/forever By using forever whenever you kill your application via preocess.kill() it will be automatically stated again. – Okky Jul 10 '14 at 04:36
  • Sounds like you just need to reflow your code logic. Why is a restart essential, can't the required functionality be achieved through other means? – Etheryte Jul 10 '14 at 05:59
  • @Nit Thats not practical for me cause the application need to start synchronous to avoid any kind of scope issues. (read config files, establish connections etc.) Then when runlevel two is reached is't in async mode and if a static config file was writen a restart is neccessary first. – Bernhard Jul 10 '14 at 11:10
  • Sounds like you're just looking for [exec-sync](http://davidwalsh.name/sync-exec). – Etheryte Jul 10 '14 at 13:19
  • See here: [nodeJS restart Itself](https://stackoverflow.com/a/46825815/4320160) – Harel Ashwal Nov 11 '17 at 20:09
  • 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
  • This question is about how to achieve a restart without an outer process manager. – Bernhard Mar 27 '19 at 08:27
  • try process.exit(1) – Sam Arul Raj T Feb 27 '20 at 18:50

1 Answers1

17

Found a working case to get node.js restarted from app itself:

Example:

// Optional part (if there's an running webserver which blocks a port required for next startup
try {
  APP.webserver.close(); // Express.js instance
  APP.logger("Webserver was halted", 'success');
} catch (e) {
  APP.logger("Cant't stop webserver:", 'error'); // No server started
  APP.logger(e, 'error');
}


// First I create an exec command which is executed before current process is killed
var cmd = "node " + APP.config.settings.ROOT_DIR + 'app.js';

// Then I look if there's already something ele killing the process  
if (APP.killed === undefined) {
  APP.killed = true;

  // Then I excute the command and kill the app if starting was successful
  var exec = require('child_process').exec;
  exec(cmd, function () {
    APP.logger('APPLICATION RESTARTED', 'success');
    process.kill();
  });
}

The only con I can see here is to loose outputs on console but if anything is logged into logfiles it's not a problem.

Bernhard
  • 4,855
  • 5
  • 39
  • 70