Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop node.js program from command line

I have a simple TCP server that listens on a port.

var net = require("net");  var server = net.createServer(function(socket) {     socket.end("Hello!\n"); });  server.listen(7777); 

I start it with node server.js and then close it with Ctrl + Z on Mac. When I try to run it again with node server.js I get this error message:

node.js:201         throw e; // process.nextTick error, or 'error' event on first tick           ^ Error: listen EADDRINUSE at errnoException (net.js:670:11) at Array.0 (net.js:771:26) at EventEmitter._tickCallback (node.js:192:41) 

Am I closing the program the wrong way? How can I prevent this from happening?

like image 366
Eleeist Avatar asked May 09 '12 19:05

Eleeist


People also ask

How do I stop a node JS process?

Method 1: Using ctrl+C key: When running a program of NodeJS in the console, you can close it with ctrl+C directly from the console with changing the code shown below: Method 2: Using process. exit() Function: This function tells Node. js to end the process which is running at the same time with an exit code.

How do you stop a node in NPM?

To stop a running npm process, press CTRL + C or close the shell window.


2 Answers

To end the program, you should be using Ctrl + C. If you do that, it sends SIGINT, which allows the program to end gracefully, unbinding from any ports it is listening on.

See also: https://superuser.com/a/262948/48624

like image 100
Brad Avatar answered Sep 16 '22 14:09

Brad


Ctrl+Z suspends it, which means it can still be running.

Ctrl+C will actually kill it.

you can also kill it manually like this:

ps aux | grep node 

Find the process ID (second from the left):

kill -9 PROCESS_ID 

This may also work

killall node 
like image 23
Jamund Ferguson Avatar answered Sep 19 '22 14:09

Jamund Ferguson