[英]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 的插件启动了一个 Node 服务器。 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:\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.
另外,如果您能告诉我如何检测端口上正在运行的内容并将其杀死。
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
的进程。 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
(强制)参数。
The process is almost identical.该过程几乎相同。 You could either kill all Node processes running on the machine:
您可以杀死机器上运行的所有 Node 进程:
killall node
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):或者也正如@jacob-groundwater使用
lsof
在下面的回答中提到的那样,您可以找到侦听端口的进程的 PID(传递-i
标志和端口以显着加快速度):
$ lsof -Pi :8080
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 1073 urname 22u IPv6 bunchanumbershere 0t0 TCP *:8080 (LISTEN)
The process ID in this case is the number underneath the PID column, which you could then pass to the kill
command:在这种情况下,进程 ID 是 PID 列下方的数字,然后您可以将其传递给
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
Again, 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
适用于 Linux、OS X
killall node
You can use lsof
get 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)".第二个数字是 PID,他们正在侦听的端口位于“(LISTEN)”之前的右侧。 Find the rogue PID and
kill -9 $PID
to terminate with extreme prejudice.找到流氓 PID 并
kill -9 $PID
以极端偏见终止。
Windows 和 GitBash 终端我需要在 Windows / Webstorm / GitBash 终端中使用这个命令
cmd "/C TASKKILL /IM node.exe /F"
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.获取所有节点进程 ID 的列表。 now you can get your process id(pid), then do:
现在您可以获取您的进程 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. -9 开关就像 Windows 上的结束任务。 it will force the process to end.
它将迫使该过程结束。 you can do:
你可以做:
kill -l
to see all switches of kill command and their comments.查看 kill 命令的所有开关及其注释。
Linux Linux
To impress your friends打动你的朋友
ps aux | grep -i node | awk '{print $2}' | xargs kill -9
But this is the one you will remember但这是你会记得的
killall node
你可以试试这个:
taskkill /IM node.exe -F
它在 Windows 10 中运行良好
taskkill /f /im node.exe
你也可以试试:
killall nodejs
you can try你可以试试
killall node
you can also try你也可以试试
killall nodejs
Am Using windows Operating system.我正在使用 windows 操作系统。
I killed all the node process and restarted the app it worked.我杀死了所有节点进程并重新启动了它工作的应用程序。
try尝试
taskkill /im node.exe
Use the following command to kill and restart node server from batch file使用以下命令从批处理文件中杀死并重新启动节点服务器
@echo off
cd "D:\sam\Projects\Node"
taskkill /IM node.exe -F
start /min cmd /C "node index.js"
goto :EOF
in windows command Type command blow:在 windows 命令中键入命令打击:
taskkill /f /im node.exe taskkill /f /im node.exe
Since you specified Windows.由于您指定了 Windows。 If you want to include this in a bat file, you might not want it to generate an error if the process is not running.
如果要将其包含在 bat 文件中,则可能不希望它在进程未运行时生成错误。
So, to prevent "ERROR: The process "node.exe" not found.", you can add a filter:因此,为了防止“错误:找不到进程“node.exe”。”,您可以添加一个过滤器:
TASKKILL /F /IM node.exe /FI "PID gt 0"
lsof -Pi :number-of-port
eg 3000 lsof -Pi :number-of-port
例如 3000
then will appear something like that on your terminal然后会在你的终端上出现类似的东西
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 26476 node 30u IPv6 63828225 0t0 TCP *:3000 (LISTEN)
kill PID-NUMBER-YOU-WANNA-KILL
eg kill 26476
例如
kill 26476
PID stands for Process ID PID 代表进程 ID
按 cmd 或 bash : Ctrl + C
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.