Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restart a node.js server

Tags:

node.js

macos

I've installed and is running a node.js server on osx. I've dled a chat module and is happily running it. I've altered some pieces and need to restart the server to see the effects.

I only know how to restart by closing the terminal window and then reopneing it and then running node chatdemo.js again.

Any way to restart without closing terminal?

Thanks.

like image 335
jsnoob Avatar asked Jul 21 '10 19:07

jsnoob


3 Answers

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

If it's daemonized then you could try:

$ ps aux | grep node
you   PID  1.5  0.2  44172  8260 pts/2    S    15:25   0:00 node app.js
$ kill -2 PID

Where PID is replaced by the number in the output of ps.

like image 101
rfunduk Avatar answered Nov 19 '22 18:11

rfunduk


During development the best way to restart server for seeing changes made is to use nodemon

npm install nodemon -g

nodemon [your app name]

nodemon will watch the files in the directory that nodemon was started, and if they change, it will automatically restart your node application.

Check nodemon git repo: https://github.com/remy/nodemon

like image 39
Nikhil Vishnu Avatar answered Nov 19 '22 17:11

Nikhil Vishnu


I had the same problem and then wrote this shell script which kills all of the existing node processes:

#!/bin/bash
echo "The following node processes were found:"
ps aux | grep " node " | grep -v grep
nodepids=$(ps aux | grep " node " | grep -v grep | cut -c10-15)

echo "OK, so we will stop these process/es now..."

for nodepid in ${nodepids[@]}
do
echo "Stopping PID :"$nodepid
kill -9 $nodepid
done
echo "Done"

After this is saved as a shell script (xxx.sh) file you might want to add it to your PATH as described here.

(Please note that this will kill all of the processes with " node " in it's name except grep's own, so I guess in some cases it may also kill some other processes with a similar name)

like image 17
Crocodile Avatar answered Nov 19 '22 16:11

Crocodile