停止node.js服务器的所有实例 [英] stop all instances of node.js server
问题描述
这是我第一次使用Node.js,我遇到这个问题:
This is my first time working with Node.js and I ran into this problem:
我已经通过IDE的插件启动了一个Node服务器。不幸的是,我不能使用IDE的终端。所以我试图从命令行运行脚本。
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.
这是问题 - 我正在使用Express模块,我的应用程序正在监听一些端口(8080)。当我从命令行启动应用程序时,会抛出以下错误:
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
我可以看到
TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING
这是因为当我尝试从IDE启动时,Node服务器已经启动。
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.
推荐答案
Windows机器: / strong>
需要杀死一个Node.js服务器,而您没有任何其他Node进程正在运行,您可以告诉您的机器杀死所有名为 code> node.exe 。这将是这样的:
taskkill /im node.exe
如果进程仍然存在,您可以通过添加 / f
标志强制进程终止: / p>
And if the processes still persist, you can force the processes to terminate by adding the /f
flag:
taskkill /f /im node.exe
如果您需要更细致的控制,并且只需要杀死在特定端口上运行的服务器,则可以使用 netstat
找到进程ID,然后发送一个kill信号给它。所以在你的情况下,端口在哪里 8080
,你可以运行以下内容:
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"
输出的第五列是进程ID:
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
然后,您可以使用 taskkill / pid 14828
。如果进程拒绝退出,那么只需在命令中添加 / f
(force)参数。
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.
该过程几乎相同。您可以杀死机器上运行的所有Node进程(如果 SIGKILL
不足),则使用 - $ SIGNAL
p>
The process is almost identical. You could either kill all Node processes running on the machine (use -$SIGNAL
if SIGKILL
is insufficient):
killall node
或者也可以使用 netstat
,您可以在端口上查找进程的PID:
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
在这种情况下,进程ID是第六列中进程名称之前的数字,然后您可以传递给 kill
命令:
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
如果进程拒绝退出,那么只需使用 -9
标志,这是一个 SIGTERM
,不能忽略:
If the process refuses to exit, then just use the -9
flag, which is a SIGTERM
and cannot be ignored:
$ kill -9 1073
这篇关于停止node.js服务器的所有实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!