Stop all instances of Node.js server
Asked Answered
M

16

312

I have started a Node server through the plugin of an IDE. Unfortunately, I cannot use the IDE's terminal. So I tried to run the script from the command line.

This is the problem - I am using the Express module and my app is listening some port (8080). When I start the app from the command line, it throws this error:

events.js:71
    throw arguments[1]; // Unhandled 'error' event
                   ^
Error: listen EADDRINUSE
    at errnoException (net.js:770:11)
    at HTTPServer.Server._listen2 (net.js:910:14)
    at listen (net.js:937:10)
    at HTTPServer.Server.listen (net.js:986:5)
    at Object.<anonymous> (C:\xampp\htdocs\node\chat\app.js:5:5)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)

Even though I am not very sure what this error could be I assumed that it's because the app is listening on a port which is already in use. So I did:

netstat -an

I can see

TCP    0.0.0.0:8080           0.0.0.0:0              LISTENING

It's because the Node server is already started when I tried to start it from the IDE.

So I want to know, how can I stop all server instances? Also if you can tell me how to detect what's running on a port and kill it.

Mirthless answered 9/2, 2013 at 19:55 Comment(3)
Sorry I dint mention that I am on windows environment. Please post commands that are relevant. ThanksMirthless
and also you can find the node.js task in your windows taskmanager. find the progress which name is Node.js:Server-side...and open it's detail info,you will find the pid and detail of your nodejs progressSclerenchyma
This is the one worked for me: superuser.com/questions/1183057/…Vampirism
V
678

Windows Machine:

Need to kill a Node.js server, and you don't have any other Node processes running, you can tell your machine to kill all processes named node.exe. That would look like this:

taskkill /im node.exe

And if the processes still persist, you can force the processes to terminate by adding the /f flag:

taskkill /f /im node.exe

If you need more fine-grained control and need to only kill a server that is running on a specific port, you can use netstat to find the process ID, then send a kill signal to it. So in your case, where the port is 8080, you could run the following:

C:\>netstat -ano | find "LISTENING" | find "8080"

The fifth column of the output is the process ID:

  TCP    0.0.0.0:8080           0.0.0.0:0              LISTENING       14828
  TCP    [::]:8080              [::]:0                 LISTENING       14828

You could then kill the process with taskkill /pid 14828. If the process refuses to exit, then just add the /f (force) parameter to the command.


MacOS machine:

The process is almost identical. You could either kill all Node processes running on the machine:

killall node

Or also as alluded to in @jacob-groundwater's answer below using lsof, you can find the PID of a process listening on a port (pass the -i flag and the port to significantly speed this up):

$ lsof -Pi :8080
COMMAND   PID      USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
node     1073    urname   22u  IPv6  bunchanumbershere      0t0  TCP *:8080 (LISTEN)

The process ID in this case is the number underneath the PID column, which you could then pass to the kill command:

$ kill 1073

If the process refuses to exit, then just use the -9 flag, which is a SIGTERM and cannot be ignored:

$ kill -9 1073

Linux machine:

Again, the process is almost identical. You could either kill all Node processes running on the machine (use -$SIGNAL if SIGKILL is insufficient):

killall node

Or also using netstat, you can find the PID of a process listening on a port:

$ netstat -nlp | grep :8080
tcp        0      0 0.0.0.0:8080         0.0.0.0:*                   LISTEN      1073/node

The process ID in this case is the number before the process name in the sixth column, which you could then pass to the kill command:

$ kill 1073

If the process refuses to exit, then just use the -9 flag, which is a SIGTERM and cannot be ignored:

$ kill -9 1073
Victorinavictorine answered 9/2, 2013 at 19:56 Comment(9)
sorry , How exactly I can use these commands? process.exit() might be in code? but server is already started. It is likely that it is started with command node app.js but not node-dev app.js. And "node killall" is not working. Am I doing it wrong? Thank youMirthless
process.exit() in your application causes the NodeJS instance to close. killall node in bash would kill all NodeJS instances running on your machine.Victorinavictorine
thanks @Victorinavictorine . I am developing on windows. Does that make killall node an invalid command because I cannot use it from command line.Mirthless
Try taskkill /IM node.exe. It will kill all processes named node.exe.Victorinavictorine
I had to use taskkill /F /IM node.exe to make it work, thanks!Camaraderie
As much as you guys are anal about duplicates and what not. You should really try to update your answer cause technically, what was given is wrongPatrilineage
These return status code 1 and fail all builds in VSTS. Any way around this?Ehrsam
For the windows solution with -/f I get "There is no running instance of the task"Vampirism
Excellent. For whatever reason a particular app won't close the express server when the terminal is closed, or the batch job is terminated, this was a great help so I didn't need to kill all the other node by guesswork.Idiotism
M
135

Works for Linux, OS X

killall node
Maegan answered 1/10, 2015 at 12:15 Comment(2)
Easy method but if any other application is running then we have to be more specific.Corwin
does not work for me. node: no process found Also when I grep for a node processes which run on the specific port, the process ID has - instead of a number itself.Bullivant
C
58

You can use lsof get the process that has bound to the required port.

Unfortunately the flags seem to be different depending on system, but on Mac OS X you can run

lsof -Pi | grep LISTEN

For example, on my machine I get something like:

mongod     8662 jacob    6u  IPv4 0x17ceae4e0970fbe9      0t0  TCP localhost:27017 (LISTEN)
mongod     8662 jacob    7u  IPv4 0x17ceae4e0f9c24b1      0t0  TCP localhost:28017 (LISTEN)
memcached  8680 jacob   17u  IPv4 0x17ceae4e0971f7d1      0t0  TCP *:11211 (LISTEN)
memcached  8680 jacob   18u  IPv6 0x17ceae4e0bdf6479      0t0  TCP *:11211 (LISTEN)
mysqld     9394 jacob   10u  IPv4 0x17ceae4e080c4001      0t0  TCP *:3306 (LISTEN)
redis-ser 75429 jacob    4u  IPv4 0x17ceae4e1ba8ea59      0t0  TCP localhost:6379 (LISTEN)

The second number is the PID and the port they're listening to is on the right before "(LISTEN)". Find the rogue PID and kill -9 $PID to terminate with extreme prejudice.

Culbreth answered 9/2, 2013 at 20:7 Comment(4)
Hi Jacob , please can you edit answer and add windows version because I am developing on windows. Thank youMirthless
I have no idea how to do it on Windows, sorry Kiran.Culbreth
This is a great answer, especially when there are multiple node servers running on different ports. I can easily distinguish the process ID's running on each port.Orellana
lsof cmd not recognized.Astir
S
40

Windows & GitBash Terminal I needed to use this command inside Windows / Webstorm / GitBash terminal

cmd "/C TASKKILL /IM node.exe /F"
Symbolic answered 22/9, 2017 at 21:17 Comment(2)
This one did it for me. ThanksSpoliate
This one did it for me as well. I found it fastest way to kill all node processes on Windows.Buchheim
E
27

if you want to kill a specific node process , you can go to command line route and type:

ps aux | grep node

to get a list of all node process ids. now you can get your process id(pid), then do:

kill -9 PID

and if you want to kill all node processes then do:

killall -9 node

-9 switch is like end task on windows. it will force the process to end. you can do:

kill -l

to see all switches of kill command and their comments.

Evermore answered 3/6, 2018 at 6:38 Comment(1)
This is the solution that worked for me in ubuntuCecilycecity
F
16

Linux

To impress your friends

ps aux | grep -i node | awk '{print $2}' | xargs  kill -9

But this is the one you will remember

killall node
Fdic answered 14/6, 2019 at 16:18 Comment(0)
S
10

You can try this:

taskkill /IM node.exe -F
Syllabism answered 28/1, 2014 at 13:35 Comment(0)
B
9

it works fine in windows 10

taskkill /f /im node.exe
Bunche answered 2/8, 2019 at 15:59 Comment(0)
R
5

If you are using Windows, follow this:

  1. Open task manager, look for this process: Task manager showing Node process - Node.js Server-side JavaScript

  2. Then just right click and "End task" it.

  3. That's it, now all the npm commands run form the start.

Rothman answered 16/9, 2018 at 19:7 Comment(0)
T
5

Multiplatform, stable, best solution:

use fkill to kill process which is taking your port:

fkill -f :8080

To install fkill use command: npm i -g fkill

Tappet answered 16/1, 2019 at 0:1 Comment(1)
Thanks Dariusz. For anyone stumbling on this in 2021, it's npm i -g fkill-cli.Koester
S
4

lsof -Pi :number-of-port e.g. 3000

then will appear something like that on your terminal

COMMAND   PID USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
node    26476 node   30u  IPv6 63828225      0t0  TCP *:3000 (LISTEN)

kill PID-NUMBER-YOU-WANNA-KILL

e.g kill 26476

PID stands for Process ID

Schuller answered 22/6, 2022 at 15:10 Comment(0)
S
3

You could also try:

killall nodejs

Samaveda answered 4/1, 2017 at 14:11 Comment(0)
B
1

Am Using windows Operating system.

I killed all the node process and restarted the app it worked.

try

taskkill /im node.exe
Berceuse answered 21/6, 2017 at 11:54 Comment(0)
I
1

in windows command Type command blow:

taskkill /f /im node.exe
Isiahisiahi answered 7/1, 2019 at 7:9 Comment(0)
C
0

Use the following command to kill and restart node server from batch file

    @echo off
cd "D:\sam\Projects\Node"
taskkill /IM node.exe -F
start /min cmd /C "node index.js"
goto :EOF
Conte answered 1/10, 2018 at 13:18 Comment(0)
M
0

Since you specified Windows. If you want to include this in a bat file, you might not want it to generate an error if the process is not running.

So, to prevent "ERROR: The process "node.exe" not found.", you can add a filter:

TASKKILL /F /IM node.exe /FI "PID gt 0"
Muleteer answered 23/7, 2021 at 19:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.