优雅地退出 node.js
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6958780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Quitting node.js gracefully
提问by Randomblue
I'm reading through the excellent online book http://nodebeginner.org/and trying out the simple code
我正在阅读优秀的在线书籍http://nodebeginner.org/并尝试使用简单的代码
var http = require("http");
function onRequest(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8888);
Now I didn't know (and I still don't know!) how to shut down node.js gracefully, so I just went ctrl+z. Now each time I try to run node server.jsI get the following error messages.
现在我不知道(我仍然不知道!)如何优雅地关闭 node.js,所以我就去了ctrl+z。现在,每次我尝试运行时node server.js,都会收到以下错误消息。
node.js:134
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: EADDRINUSE, Address already in use
at Server._doListen (net.js:1100:5)
at net.js:1071:14
at Object.lookup (dns.js:153:45)
at Server.listen (net.js:1065:20)
at Object.<anonymous> (/Users/Bob/server.js:7:4)
at Module._compile (module.js:402:26)
at Object..js (module.js:408:10)
at Module.load (module.js:334:31)
at Function._load (module.js:293:12)
at Array.<anonymous> (module.js:421:10)
So, two questions:
所以,两个问题:
1) How do I shut down node.js gracefully?
1) 如何优雅地关闭 node.js?
2) How do I repair the mess I've created?
2) 如何修复我造成的混乱?
采纳答案by avstrallen
Use Ctrl+Cto exit the node process gracefully
To clean up the mess depends on your platform, but basically you need to find the remains of the process in which node was running and kill it.
For example, on Unix:
ps -ax | grep nodewill give you an entry like:1039 ttys000 0:00.11 node index.jswhere
index.jsis the name of your node file.In this example, 1039 is the process id (yours will be different), so
kill -9 1039will end it, and you'll be able to bind to the port again.
使用Ctrl+C优雅退出节点进程
清理混乱取决于您的平台,但基本上您需要找到运行节点的进程的剩余部分并杀死它。
例如,在 Unix:
ps -ax | grep node会给你一个条目,如:1039 ttys000 0:00.11 node index.jsindex.js您的节点文件的名称在哪里。在此示例中,1039 是进程 ID(您的将不同),因此
kill -9 1039将结束它,您将能够再次绑定到端口。
回答by EhevuTov
I currently use Node's event system to respond to signals. Here's how I use the Ctrl-C (SIGINT) signal in a program:
我目前使用 Node 的事件系统来响应信号。以下是我在程序中使用 Ctrl-C (SIGINT) 信号的方法:
process.on( 'SIGINT', function() {
console.log( "\nGracefully shutting down from SIGINT (Ctrl-C)" );
// some other closing procedures go here
process.exit( );
})
You were getting the 'Address in Use' error because Ctrl-Z doesn't kill the program; it just suspends the process on a unix-like operating system and the node program you placed in the background was still bound to that port.
您收到“正在使用的地址”错误,因为 Ctrl-Z 不会终止程序;它只是在类 Unix 操作系统上暂停进程,而您放置在后台的节点程序仍绑定到该端口。
On Unix-like systems, [Control+Z] is the most common default keyboard mapping for the key sequence that suspends a process (SIGTSTP).[3] When entered by a user at their computer terminal, the currently running foreground process is sent a SIGTSTP signal, which generally causes the process to suspend its execution. The user can later continue the process execution by typing the command 'fg' (short for foreground) or by typing 'bg' (short for background) and furthermore typing the command 'disown' to separate the background process from the terminal.1
在类 Unix 系统上,[Control+Z] 是挂起进程的键序列 (SIGTSTP) 的最常见默认键盘映射。 [3] 当用户在他们的计算机终端输入时,当前运行的前台进程会被发送一个 SIGTSTP 信号,这通常会导致进程暂停其执行。用户稍后可以通过键入命令“fg”(前台的缩写)或键入“bg”(后台的缩写)并进一步键入命令“disown”以将后台进程与终端分开来继续进程执行。1
You would need to kill your processes by doing a kill <pid>or 'killall -9 node' or the like.
您需要通过执行 akill <pid>或 'killall -9 node' 等来终止您的进程。
回答by jkingyens
As node.js is an event-driven runtime the most graceful exit is to exhaust the queue of pending events. When the event queue is empty the process will end. You can ensure the event queue is drained by doing things such as clearing any interval timers that are set and by closing down any servers with open socket connections. It gets trickier when using 3rd party modules because you are at the mercy of whether the module author has taken care to gracefully drain the pending events it created. This might not be the most practicalway to exit a node.js process as you will spend a lot of effort tracking down 'leaked' pending events, but it isthe most graceful I think.
由于 node.js 是一个事件驱动的运行时,最优雅的退出是耗尽待处理事件的队列。当事件队列为空时,该过程将结束。您可以通过清除任何设置的间隔计时器以及关闭任何具有打开套接字连接的服务器来确保事件队列被排空。使用 3rd 方模块时会变得更加棘手,因为您受制于模块作者是否已注意优雅地排出它创建的挂起事件。这可能不是退出 node.js 进程的最实用方法,因为您将花费大量精力来追踪“泄露”的待处理事件,但我认为这是最优雅的方法。
回答by Vietnhi Phuvan
Type either
输入
process.exit()
or
或者
.exit
to exit nodegracefully.
node优雅地退出。
Hitting Control+ Ctwice will force an exit.
按Control+C两次将强制退出。
回答by Gabriel Llamas
1) How do I shut down node.js gracefully?
1) 如何优雅地关闭 node.js?
Listening for a SIGINT signal. On Windows you need to listen for a ctrl-c with the readline module.
监听 SIGINT 信号。在 Windows 上,您需要使用 readline 模块监听 ctrl-c。
I've written my own solution to provide an application a graceful shutdown and the usage of domains: grace. It's worth to have a look.
我已经编写了自己的解决方案来为应用程序提供优雅的关闭和域的使用:grace。值得一看。

