Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

javascript - 停止所有node.js服务器实例(stop all instances of node.js server)

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.

(我已经通过IDE的插件启动了节点服务器。)

Unfortunately, I cannot use the IDE's terminal.

(不幸的是,我无法使用IDE的终端。)

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).

(这是问题所在-我正在使用Express模块??,而我的应用正在监听某个端口(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

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.

(另外,如果您能告诉我如何检测端口上正在运行的内容并杀死它。)

  ask by Kiran Ambati translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Windows Machine:(Windows机器:)

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 .

(需要终止Node.js服务器,并且您没有运行任何其他Node进程,则可以告诉您的计算机node.exe所有名为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:

(如果进程仍然存在,则可以通过添加/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 netstat to find the process ID, then send a kill signal to it.

(如果您需要更细粒度的控制,并且只需要终止在特定端口上运行的服务器,则可以使用netstat查找进程ID,然后向其发送终止信号。)

So in your case, where the port is 8080 , you could run the following:

(因此,在您的情况下,端口为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 .

(然后,您可以使用taskkill /pid 14828该进程。)

If the process refuses to exit, then just add the /f (force) parameter to the command.

(如果该进程拒绝退出,则只需将/f (force)参数添加到命令中。)


Linux machine:(Linux机器:)

The process is almost identical.

(这个过程几乎是相同的。)

You could either kill all Node processes running on the machine (use -$SIGNAL if SIGKILL is insufficient):

(您可以杀死计算机上运行的所有Node进程(如果SIGKILL不足,请使用-$SIGNAL ):)

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 kill command:

(在这种情况下,进程ID是第六列中进程名称之前的数字,然后您可以将其传递给kill命令:)

$ kill 1073

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

(如果该进程拒绝退出,则只需使用-9标志,它是SIGTERM ,不能忽略:)

$ kill -9 1073

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...