Restart a node.js app from code level

16,861

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.

Share:
16,861

Related videos on Youtube

Bernhard
Author by

Bernhard

Code to develop solutions and not to write code - I put a lot of emphasis on thinking along with measurable transparency and QA. I'm currently working on adaptive Industry 4.0 solutions and I'm happy to bring my accumulated experience and skills from electrical engineering (RWE Group & COBI as first MA and today part of Bosch Group) to the table. In addition to the technical skillset, it is important to me to understand problems and requirements of existing and potential new partners, to translate these into requirements and solutions that are convincing and are the cornerstone for long-term partnerships at eye level. I can bring in my experience especially in the area of "courage for new things" and on "the green field" and I am happy when such projects reach calmer waters and can be transferred into a regular operation - supervised by motivated teams.

Updated on September 15, 2022

Comments

  • Bernhard
    Bernhard over 1 year

    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?

    • Mritunjay
      Mritunjay almost 10 years
      If I got your problem correctly, I think nodmon will help you.
  • smartworld-dm
    smartworld-dm almost 5 years
    What is APP here?
  • Bernhard
    Bernhard over 4 years
    @smartworld-dm APP ist the express.js instance
  • Darryl RN
    Darryl RN over 4 years
    what is process there?
  • Nils Fahle
    Nils Fahle over 4 years
    @DarrylR.Norbert process is a global object that provides information about, and control over, the current Node.js process. You can find more at nodejs.org/api/process.html