showing results for - "nodejs restart process"
Alessandro
15 Oct 2016
1Generally process managers are used to (automatically) restart processes, such as monit, PM2, nodemon, forever, etc.
2
3However, you could restart from the process itself by simply spawning a detached child process that waits some period of time and then executes the same script. You could do this as a combination of two commands, one to sleep and the other the current command line, or you could simply incorporate the sleep into your script. An example of the latter:
4
5var spawn = require('child_process').spawn;
6
7(function main() {
8
9  if (process.env.process_restarting) {
10    delete process.env.process_restarting;
11    // Give old process one second to shut down before continuing ...
12    setTimeout(main, 1000);
13    return;
14  }
15
16  // ...
17
18  // Restart process ...
19  spawn(process.argv[0], process.argv.slice(1), {
20    env: { process_restarting: 1 },
21    stdio: 'ignore'
22  }).unref();
23})();
24On Windows, you may need to add detached: true to your spawn() configuration object though. For *nix, this usually shouldn't be necessary.
25
26One thing to keep in mind though is that any restarted process won't have access to the terminal anymore, if the original process was started in the foreground.
27
28Also, you could eliminate the delay and process.env checking if your script does not use any resources that can only be used by at most one process at any given time.
29
30One final note: if your process crashes abnormally, due to memory exhaustion, triggering a C++ assertion, a V8 bug, etc., your process won't restart obviously (even if you have an 'unhandledException' event handler set for process). To account for these situations you pretty much need some sort of external mechanism to restart the process.
queries leading to this page
nodejs process restarthow to restart the process in node after process existhow to restart a node js script after process exitnode restart jsnodejs restartjs node process restart itselfnode process restart itselfjavascript node restartrestart node js applicationrestart app nodejsstop and restart node from noderestart node cmdrestart node js apprestart nodejs apprestart node from node itselfhow to restart nodejs appstop and restart node server restart node jshow to restart node properlyhow to restart nodejs serverhow to start 2fstop 2frestart a node js server processprocess restart nodenode server restartnode js restartdo you have to restart for node to installnode js restart scriptnode restartrestart node processnodejs restart node app node restart processhow to disconnect or restart node restart node jsrestart node process linux terminalnode restart own processrestart process in nodemake node restart on changerestart node js server terminallinux restart nodenode kill nodejs on restartjavascript node restart itselfauto restart node js servernode restart commandrestarting node serviceapp restarts because of node processnodejs restart appprocess restart nodejsnodejs restart current scripthow to restart the process in node after process exitnodejs restart processruning my node app on restartsystem restart nodejswhy node js application restart automaticallyrestart node process linuxnodejs restart serverhow to disconnect or restart nodeprocess restart nodejsnpm restart node serverrestart node servernode restart servernodejs restart automaticallynode server restart automaticallynode js restart commandnode js restart app cmdnode js restartnode restart application node js restart apphow to restart node jsrestarting node kill and restart nodenode red restart commandrestart app node jsdo i have to restart after installing nodehow to restart noderestart process nodejsnodejs restart servicehow to restart a nodejs serverrestart node js processnode restart linuxrestart node redrestart node process programmaticallynode red restartrestart nodejs processrestart nodenode restart from apprestart node js coderestart node programnode restart apphw to automatically restart a nodehow to restart node js server from terminalhow to restart node jsrestart node script from scriptrestart nodejs from inside itselfnode process restartrestart all nodejs processnode js command for restart applicationhow o restart an application in node jsforce node to restartrestart nodejs server withought going downrestart node apprestart nodejs serverrestart node servicehow to restart a node processrestart node js serverrestart nodejshow to restart node servernode js quit and restartrestart node from noderestart node automaticallyrestart a node process from codeapp restart nodejsnodejs restart process