本文翻译自:Stop node.js program from command line
I have a simple TCP server that listens on a port. 我有一个简单的TCP服务器,可以监听端口。
var net = require("net");
var server = net.createServer(function(socket) {
socket.end("Hello!\n");
});
server.listen(7777);
I start it with node server.js
and then close it with Ctrl + Z on Mac. 我用node server.js
启动它,然后在Mac上用Ctrl + Z关闭它。 When I try to run it again with node server.js
I get this error message: 当我尝试使用node server.js
再次运行它时,我收到以下错误消息:
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: listen EADDRINUSE
at errnoException (net.js:670:11)
at Array.0 (net.js:771:26)
at EventEmitter._tickCallback (node.js:192:41)
Am I closing the program the wrong way? 我以错误的方式关闭程序吗? How can I prevent this from happening? 我怎样才能防止这种情况发生?
#1楼
参考:https://stackoom.com/question/I9og/从命令行停止node-js程序
#2楼
To end the program, you should be using Ctrl + C . 要结束程序,您应该使用Ctrl + C. If you do that, it sends SIGINT
, which allows the program to end gracefully, unbinding from any ports it is listening on. 如果你这样做,它会发送SIGINT
,它允许程序正常结束,从正在侦听的任何端口解除绑定。
See also: https://superuser.com/a/262948/48624 另见: https : //superuser.com/a/262948/48624
#3楼
Ctrl + Z suspends it, which means it can still be running. Ctrl + Z暂停它,这意味着它仍然可以运行。
Ctrl + C will actually kill it. Ctrl + C实际上会杀死它。
you can also kill it manually like this: 你也可以像这样手动杀死它:
ps aux | grep node
Find the process ID (second from the left): 找到进程ID(左起第二个):
kill -9 PROCESS_ID
This may also work 这也可行
killall node
#4楼
$ sudo killall node
in another terminal works on mac , while killall node
not working: 另一个终端中的$ sudo killall node
在mac上工作 ,而killall node
不工作:
$ killall node
No matching processes belonging to you were found
#5楼
你可以输入.exit
来退出节点js REPL
#6楼
If you are running Node.js interactively (the REPL): 如果您以交互方式运行Node.js(REPL):
Ctrl + C will take back you to > prompt then type: Ctrl + C将收回>提示然后键入:
process.exit()
or just use Ctrl + D . 或者只使用Ctrl + D.