Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I restart a Node.js app from within itself (programmatically)?

Tags:

How can I create an app that can restart itself? I want to create an app that sets up a web-admin which can restart itself. Is this possible? If so, how? I was thinking this might be possible with the process global variable that is built into node.

like image 245
LordZardeck Avatar asked Feb 20 '12 07:02

LordZardeck


People also ask

How do I restart an app with node js?

Run the dev scriptTo manually restart your Node. js application you can type rs in the command line. Important: If your application writes to files, like updating a config file, nodemon will prematurely restart your application.

What is used to programmatically restart a node JS application?

Nodemon allows you to automate the process of restarting Node applications when you make changes to the underlying files.

How do I restart a node js server?

In this case, if we make any changes to the project then we will have to restart the server by killing it using CTRL+C and then typing the same command again. It is a very hectic task for the development process.


2 Answers

LK"I

It is possible without external dependencies:

console.log("This is pid " + process.pid); setTimeout(function () {     process.on("exit", function () {         require("child_process").spawn(process.argv.shift(), process.argv, {             cwd: process.cwd(),             detached : true,             stdio: "inherit"         });     });     process.exit(); }, 5000); 

source : https://gist.github.com/silverwind/d0802f7a919ae86ff25e

like image 54
Harel Ashwal Avatar answered Oct 06 '22 15:10

Harel Ashwal


I have run Forever several times and it is easy to get started with. Check it out at: https://github.com/nodejitsu/forever

like image 37
Marcus Granström Avatar answered Oct 06 '22 13:10

Marcus Granström