Javascript 停止 node.js 服务器的所有实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14790910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
stop all instances of node.js server
提问by Kiran Ambati
This is my first time working with Node.js and I ran into this problem:
这是我第一次使用 Node.js,我遇到了这个问题:
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.
我已经通过 IDE 的插件启动了一个 Node 服务器。不幸的是,我无法使用 IDE 的终端。所以我尝试从命令行运行脚本。
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:
这就是问题所在 - 我正在使用 Express 模块并且我的应用程序正在侦听某个端口 (8080)。当我从命令行启动应用程序时,它会引发此错误:
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.
这是因为当我尝试从 IDE 启动它时,Node 服务器已经启动。
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.
所以我想知道,如何停止所有服务器实例?另外,如果您能告诉我如何检测端口上正在运行的内容并杀死它。
回答by hexacyanide
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:
需要杀死一个 Node.js 服务器,并且您没有任何其他 Node 进程正在运行,您可以告诉您的机器杀死所有名为 .js 的进程node.exe。那看起来像这样:
taskkill /im node.exe
And if the processes still persist, you can force the processes to terminate by adding the /fflag:
如果进程仍然存在,您可以通过添加/f标志来强制进程终止:
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 netstatto 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:
如果您需要更细粒度的控制并且只需要杀死在特定端口上运行的服务器,则可以使用netstat找到进程 ID,然后向其发送终止信号。因此,在您的情况下,端口所在的位置8080,您可以运行以下命令:
C:\>netstat -ano | find "LISTENING" | find "8080"
The fifth column of the output is the process ID:
输出的第五列是进程 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.
然后,您可以使用taskkill /pid 14828. 如果进程拒绝退出,则只需/f在命令中添加(force) 参数即可。
Linux machine:
Linux机器:
The process is almost identical. You could either kill all Node processes running on the machine (use -$SIGNALif SIGKILLis insufficient):
过程几乎相同。您可以杀死机器上运行的所有 Node 进程(-$SIGNAL如果SIGKILL不足,请使用):
killall node
Or also using netstat, you can find the PID of a process listening on a port:
或者也可以使用netstat,您可以找到侦听端口的进程的 PID:
$ 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 killcommand:
在这种情况下,进程 ID 是第六列中进程名称之前的数字,然后您可以将其传递给kill命令:
$ kill 1073
If the process refuses to exit, then just use the -9flag, which is a SIGTERMand cannot be ignored:
如果进程拒绝退出,那么只需使用-9标志,它是一个SIGTERM不能被忽略的标志:
$ kill -9 1073
回答by zag2art
The fastest way is
最快的方法是
killall node
Works with Linux, OS X
适用于 Linux、OS X
回答by Jacob Groundwater
You can use lsofget the process that has bound to the required port.
您可以使用lsof获取已绑定到所需端口的进程。
Unfortunately the flags seem to be different depending on system, but on Mac OS X you can run
不幸的是,标志似乎因系统而异,但在 Mac OS X 上您可以运行
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 $PIDto terminate with extreme prejudice.
第二个数字是 PID,他们正在侦听的端口位于“(LISTEN)”之前的右侧。找到流氓PID并kill -9 $PID以极端偏见终止。
回答by Enkode
Windows & GitBash TerminalI needed to use this command inside Windows / Webstorm / GitBash terminal
Windows & GitBash 终端我需要在 Windows/Webstorm/GitBash 终端中使用这个命令
cmd "/C TASKKILL /IM node.exe /F"
回答by Ali_Hr
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:
获取所有节点进程 ID 的列表。现在你可以得到你的进程 id(pid),然后做:
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:
-9 开关就像 Windows 上的结束任务。它将迫使进程结束。你可以做:
kill -l
to see all switches of kill command and their comments.
查看 kill 命令的所有开关及其注释。
回答by Ali_Hr
You can try this:
你可以试试这个:
taskkill /IM node.exe -F
回答by Alex Nolasco
Linux
Linux
To impress your friends
为了给你的朋友留下深刻印象
ps aux | grep -i node | awk '{print }' | xargs kill -9
But this is the one you will remember
但这是你会记得的
killall node
回答by Emeka Mbah
You could also try:
你也可以试试:
killall nodejs
killall nodejs
回答by Neil Bannet
If you are using windows, follow this:
如果您使用的是 Windows,请按照以下步骤操作:
1) Open task manager, look for this process: http://prntscr.com/kv3uqx
1)打开任务管理器,找到这个进程:http: //prntscr.com/kv3uqx
2) Then just right click and "End task" it.
2)然后只需右键单击并“结束任务”即可。
3) That's it, now all the npm commands run form the start.
3) 就是这样,现在所有 npm 命令都从开始运行。
Hope it help somebody!
希望它可以帮助某人!
Cheers
干杯
回答by Thavaprakash Swaminathan
it works fine in windows 10
它在 Windows 10 中运行良好
taskkill /f /im node.exe

