Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restart Node.js server programmatically

Tags:

How to restart Node.js server from code? For example, if using expressjs framework,

app.get('/restart', function (req, res, next) {
//Code to restart a server
})

I want to restart server from app like this, without going to console etc. How to do it?

like image 999
user2900890 Avatar asked May 20 '14 17:05

user2900890


People also ask

How do I restart a node js server?

If it's just running (not a daemon) then just use Ctrl-C.

How do I restart node server automatically?

Running non-Node code While Nodemon is running, we can manually restart our application. So instead of stopping and restarting Nodemon, we can just type rs and press enter, and Nodemon will restart the server or the running process for us.

What is used to programmatically restart a node JS application?

By combining npm restart and child_process. exec() it appears possible to restart the server programmatically. And since it allows for several scripts to be run for stopping, restarting, and starting this option would work even for multi-process servers or otherwise complex servers where simply calling process.

How do I rerun node JS?

If it's just running (not a daemon) then just use Ctrl-C . Where PID is replaced by the number in the output of ps . You could also use "killall -2 node", which has the same effect.


2 Answers

I use forever in order to start and monitoring application. So the restart function like this:

app.get('/restart', function (req, res, next) {
  process.exit(1);
});

After the shutdown of server, forever will restart service.

console:

Express server listening on port 3000 in development mode
error: Forever detected script exited with code: 1
error: Forever restarting script for 2 time
Express server listening on port 3000 in development mode
like image 61
germanlinux Avatar answered Sep 21 '22 23:09

germanlinux


By combining npm restart and child_process.exec() it appears possible to restart the server programmatically. And since it allows for several scripts to be run for stopping, restarting, and starting this option would work even for multi-process servers or otherwise complex servers where simply calling process.exit() isn't viable.

like image 32
ElJay Avatar answered Sep 19 '22 23:09

ElJay