停止 node.js 服务器的所有实例 [英] stop all instances of node.js server

查看:51
本文介绍了停止 node.js 服务器的所有实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用 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:xampphtdocs
odechatapp.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 Machine:

需要杀死一个 Node.js 服务器,并且你没有任何其他 Node 进程在运行,你可以告诉你的机器杀死所有名为 node.exe 的进程.看起来像这样:

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

如果进程仍然存在,您可以通过添加 /f 标志强制进程终止:

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,然后向它发送终止信号.因此,在您的情况下,端口为 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(强制)参数即可.

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 进程:

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

killall node

或者也如 @jacob-groundwater 下面的回答中提到的那样,使用 lsof,您可以找到侦听端口的进程的 PID(传递 -i 标志和端口以显着加快速度):

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)

在这种情况下,进程 ID 是 PID 列下面的数字,然后您可以将其传递给 kill 命令:

The process ID in this case is the number underneath the PID 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


Linux 机器:

同样,过程几乎相同.您可以杀死机器上运行的所有 Node 进程(如果 SIGKILL 不够用,请使用 -$SIGNAL):


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

或者也可以使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆