How to restart node.js server

I installed and run node.js server in osx. I launched the chat module and am happy to run it. I changed some parts and you need to restart the server to see the effects.

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

Any way to restart without closing the terminal?

Thank.

+57
macos
Jul 21 '10 at 19:19
source share
8 answers

If it is just running (and not a daemon), just use Ctrl-C .

If it is demonized, you can 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 the PID is replaced by the output number ps .

+68
Jul 21 '10 at 19:26
source share
β€” -

During development, the best way to restart the server to see the changes made is to use nodemon

npm install nodemon -g

nodemon [your application name]

nodemon will look at the files in the directory in which nodemon was launched, and if they are changed, it will automatically restart your node application.

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

+37
Sep 24 '12 at 12:45
source share

In this case, you often restart the node.js server because it is under active development and you are constantly making changes. There is a big hot reload script that will handle this for you by looking at all your .js files and restarting your node.js server if any of these files has changed. Just a ticket for quick development and testing.

The script and explanation of how to use it are here Draco Blue .

+16
Nov 23 '10 at 1:23
source share

I had the same problem and I wrote this shell script that kills all 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 that, as a shell script file (xxx.sh), you can add it to your PATH as described here .

(Note that this will kill all processes with a "node" in it except grep own, so I assume that in some cases it can also kill some other processes with a similar name)

+10
May 22 '11 at 17:41
source share

Using "kill -9 [PID]" or "killall -9 node" worked for me, where "kill -2 [PID]" didn't work.

+2
Dec 19 '12 at 16:36
source share

If I just run the node application from the console (I don’t use forever, etc.), I use control + C, not sure if OSX has the same key combination as before, but much faster than finding the process id and its killing, you can also add the following code to the chat application that you are using, and then enter "exit" on the console when you want to close the application.

 process.stdin.resume(); process.stdin.setEncoding('utf8'); process.stdin.on('data', function(data) { if (data == 'exit\n') process.exit(); }); 
+2
Feb 15 '13 at 6:14
source share

I understand that my comment is related to windows, but someone might be helpful. To win, run in cmd:

 wmic process where "commandline like '%my_app.js%' AND name='node.exe' " CALL Terminate 

you can run the application again:

 node my_app.js 

You can also use it in a batch file with quotes:

 wmic process where "commandline like '%%my_app.js%%' AND name='node.exe' " CALL Terminate node my_app.js 
+2
Nov 13 '13 at 22:04
source share

Say "nodemon" will answer the question.

But about how to just kill (all) node daemon (s), the following works for me:

 pkill -HUP node 
+2
Mar 04 '14 at 5:39 on
source share



All Articles