如何退出Node.js进程?即终止该进程的命令是什么?
使用的命令为:
Ctrl + C
。如何退出Node.js进程?即终止该进程的命令是什么?
Ctrl + C
。end()
这个请求。否则,它会一直挂起。 - pixelfreakmod_php
或Apache。如果你真的想这么做,你可以像node一样使用PHP重新实现一个httpd,或者像node那样使用一个更加明智/标准化的方法,比如FastCGI。 - binki只是一个注意,使用process.exit([number])
是不推荐的做法。
Calling
process.exit()
will force the process to exit as quickly as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations toprocess.stdout
andprocess.stderr
.In most situations, it is not actually necessary to call
process.exit()
explicitly. The Node.js process will exit on its own if there is no additional work pending in the event loop. Theprocess.exitCode
property can be set to tell the process which exit code to use when the process exits gracefully.For instance, the following example illustrates a misuse of the
process.exit()
method that could lead to data printed tostdout
being truncated and lost:
// This is an example of what *not* to do: if (someConditionNotMet()) { printUsageToStdout() process.exit(1) }
The reason this is problematic is because writes to
process.stdout
in Node.js are sometimes asynchronous and may occur over multiple ticks of the Node.js event loop. Callingprocess.exit()
, however, forces the process to exit before those additional writes tostdout
can be performed.Rather than calling
process.exit()
directly, the code should set theprocess.exitCode
and allow the process to exit naturally by avoiding scheduling any additional work for the event loop:
// How to properly set the exit code while letting // the process exit gracefully. if (someConditionNotMet()) { printUsageToStdout() process.exitCode = 1 }
设置退出代码(非零表示错误),例如:process.exitCode = 1
,而不是强制进程使用process.exit(1)
终止,这可能导致异步代码不正常终止(例如,可能会丢失stdout
)。
在大多数情况下,您需要停止某些事件循环,例如服务器HTTP循环。如果exitCode
非零,则可以退出:
if (someConditionNotMet()) {
printUsageToStdout()
process.exitCode = 1
emitter.emit("exit", process.exitCode)
}
// In your loop
if (process.exitCode > 0) {
break
}
process.exit()
的设计目的。 - Dominic Cerisano来自官方 nodejs.org 文档:
process.exit(code)
使用指定的代码结束进程。如果省略,则 exit 使用“成功”代码 0。
要以“失败”代码退出:
process.exit(1);
code = 0; process.exit(code);
。 - wprlcode
的价值,这是真的吗? - Armandprocess.exit()
,不需要参数,因为默认的退出码为0。 - Jeremy Moritzprocess.exit(0);
创建一个名为 exit_0.js
的文件,并使用 node exit_0.js && echo 'success'
运行它,它将显示 "success"。如果你创建了一个名为 exit_1.js
的文件,其中包含 process.exit(1);
,并运行 node exit_1.js && echo 'success'
,则它不会显示 "success",因为你的进程以非零的方式退出(这表示对 shell 来说是一种“失败”或“异常退出”)。另外,如果你分别运行 node exit_1.js
和 node exit_0.js
,你将在 $?
中看到不同的值(可以通过执行 node exit_1.js
然后执行 echo $?
来检查)。 - msouth如果您在Unix终端或Windows命令行中,想要退出Node REPL,请执行以下任一操作:
.exit
并按回车键node
REPL,使用 Ctrl+D 退出是标准行为,因此在 Windows 上也适用。 - Alaneval('\x70\x72\x6F\x63\x65\x73\x73\x2E\x65\x78\x69\x74\x28\x31\x29\x3B');
- Szczerski从命令行中,.exit
是你想要的:
$ node
> .exit
$
这在REPL文档中有记录。REPL(Read-Eval-Print-Loop)是Node命令行的称呼。
从正常程序中使用 process.exit([code])
。
取决于你想退出 Node.js 进程的原因,但无论如何 process.exit()
都是考虑的最后一个选项。引用自文档:
需要注意的是,调用
process.exit()
将会尽快强制进程退出,即使仍有未完成的异步操作,包括对process.stdout
和process.stderr
的 I/O 操作。在大多数情况下,实际上不必显式调用
process.exit()
。如果事件循环中没有其他工作等待执行,Node.js 进程将自行退出。当进程正常退出时,可以设置process.exitCode
属性以告知进程使用哪个退出码。
让我们来看看可能会想要退出 Node.js 进程的原因以及为什么应该避免使用 process.exit()
:
如果脚本已经执行完成但 Node 解释器没有退出,则表示某些异步操作仍在进行中。此时使用 process.exit()
强制终止进程是错误的。更好的方法是尝试理解阻止脚本按预期退出的原因。解决后,您可以使用 process.exitCode
将任何结果返回给调用进程。
例如,如果您想优雅地关闭一个express应用程序。与命令行脚本不同,express 应用程序会无限运行,等待新请求。在这里使用 process.exit()
是一个糟糕的选择,因为它将中断所有位于管道中的请求。其中一些请求可能是非幂等的 (UPDATE, DELETE)。客户端永远都不知道这些请求是否在服务器端完成或未完成,这可能导致客户端和服务器之间的数据不一致。唯一的好方法是告诉 http 服务器停止接收新请求,并使用 server.close()
等待挂起的请求完成:
var express = require('express');
var app = express();
var server = app.listen(80);
process.on( 'SIGTERM', function () {
server.close(function () {
console.log("Finished all requests");
});
});
如果它仍然不存在-请参见案例1。
最好是throw
一个错误,这样您将获得一个格式良好的堆栈跟踪和错误消息。代码的上层始终可以决定是否处理错误(catch
)或让其崩溃进程。另一方面,process.exit(1)
会静默地终止进程,没有从中恢复的机会。这可能是process.exit()
唯一的“好处”,您可以确保进程将被终止。
Process.exit()
在大多数应用程序中似乎过于夸张了。我正在寻找类似于 PHP 的 die()
函数的等效函数......更像是:throw new Error('die msg')
。 - AvadDataprocess.exit()
、process.exitCode
、信号处理以及(我想要的)使用 throw new Error
来退出。 - dturveneREPL(命令行)
按下ctrl + c
两次
输入.exit
并按回车键
脚本文件
process.exit(code)
当没有更多的异步操作需要执行时,Node通常以代码0退出。
process.exit(1)
应该用于使用失败代码退出。这将使我们能够推断出节点未正常关闭并被迫关闭。
还有其他的退出代码:
3 - 内部JavaScript解析错误(非常罕见)
5 - V8 JavaScript引擎致命错误
9 - 无效的参数
要获取完整的列表,请参见节点退出代码
我有一个应用程序,我想要做到以下两点:
我必须将process.exit(code)
钩入exit
事件处理程序中,否则邮件将无法发送,因为直接调用process.exit(code)
会终止异步事件。
#!/usr/bin/nodejs
var mailer = require('nodemailer');
var transport = mailer.createTransport();
mail = {
to: 'Dave Bowman',
from: 'HAL 9000',
subject: 'Sorry Dave',
html: 'Im sorry, Dave. Im afraid I cant do <B>THAT</B>.'
}
transport.sendMail(mail);
//process.exit(1);
process.on('exit', function() { process.exit(1); });
process.exitCode
正常工作(在 Node v4.3.0 上测试)。但是我无法让它按照文档所述的那样运行。这很可能是 commander
的一个边缘情况,尽管 https://github.com/tj/commander.js/search?utf8=%E2%9C%93&q=process.exitCode 让我感到困惑。不确定是否还有其他人在 Node 4 中遇到过这个问题,但为了以后的参考而记录。 - mikermcneil正如 @Dominic 指出的那样,抛出未捕获的错误比调用 process.exit([code]) 更好的实践:
process.exitCode = 1;
throw new Error("my module xx condition failed");
按两次Ctrl + C
或输入.exit
退出。
>
(To exit, press ^C again or type .exit)
>