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