信号事件
¥Signal events
当 Node.js 进程收到信号时,则将触发信号事件。请参阅 signal(7)
以获取标准 POSIX 信号名称的列表,例如 'SIGINT'
、'SIGHUP'
等。
¥Signal events will be emitted when the Node.js process receives a signal. Please
refer to signal(7)
for a listing of standard POSIX signal names such as
'SIGINT'
, 'SIGHUP'
, etc.
信号在 Worker
线程上不可用。
¥Signals are not available on Worker
threads.
信号句柄将接收信号的名称('SIGINT'
、'SIGTERM'
等)作为第一个参数。
¥The signal handler will receive the signal's name ('SIGINT'
,
'SIGTERM'
, etc.) as the first argument.
每个事件的名称将是信号的大写通用名称(例如 'SIGINT'
表示 SIGINT
信号)。
¥The name of each event will be the uppercase common name for the signal (e.g.
'SIGINT'
for SIGINT
signals).
import process from 'node:process';
// Begin reading from stdin so the process does not exit.
process.stdin.resume();
process.on('SIGINT', () => {
console.log('Received SIGINT. Press Control-D to exit.');
});
// Using a single function to handle multiple signals
function handle(signal) {
console.log(`Received ${signal}`);
}
process.on('SIGINT', handle);
process.on('SIGTERM', handle);
const process = require('node:process');
// Begin reading from stdin so the process does not exit.
process.stdin.resume();
process.on('SIGINT', () => {
console.log('Received SIGINT. Press Control-D to exit.');
});
// Using a single function to handle multiple signals
function handle(signal) {
console.log(`Received ${signal}`);
}
process.on('SIGINT', handle);
process.on('SIGTERM', handle);
-
'SIGUSR1'
由 Node.js 保留以启动 debugger。可以安装监听器,但这样做可能会干扰调试器。¥
'SIGUSR1'
is reserved by Node.js to start the debugger. It's possible to install a listener but doing so might interfere with the debugger. -
'SIGTERM'
和'SIGINT'
在非 Windows 平台上具有默认的句柄,其在使用代码128 + signal number
退出之前重置终端模式。如果这些信号之一安装了监听器,则其默认行为将被删除(Node.js 将不再退出)。¥
'SIGTERM'
and'SIGINT'
have default handlers on non-Windows platforms that reset the terminal mode before exiting with code128 + signal number
. If one of these signals has a listener installed, its default behavior will be removed (Node.js will no longer exit). -
'SIGPIPE'
默认情况下忽略。它可以安装监听器。¥
'SIGPIPE'
is ignored by default. It can have a listener installed. -
'SIGHUP'
在 Windows 上是在关闭控制台窗口时生成,在其他平台上是在各种类似条件下生成。参见信号(7)。它可以安装监听器,但是 Node.js 将在大约 10 秒后被 Windows 无条件地终止。在非 Windows 平台上,SIGHUP
的默认行为是终止 Node.js,但一旦安装了监听器,则其默认行为将被删除。¥
'SIGHUP'
is generated on Windows when the console window is closed, and on other platforms under various similar conditions. Seesignal(7)
. It can have a listener installed, however Node.js will be unconditionally terminated by Windows about 10 seconds later. On non-Windows platforms, the default behavior ofSIGHUP
is to terminate Node.js, but once a listener has been installed its default behavior will be removed. -
'SIGTERM'
Windows 上不支持,可以监听。¥
'SIGTERM'
is not supported on Windows, it can be listened on. -
所有平台都支持来自终端的
'SIGINT'
,并且通常可以使用 Ctrl+C 生成(尽管这可能是可配置的)。当 终端原始模式 使能并且使用 Ctrl+C 时不会生成。¥
'SIGINT'
from the terminal is supported on all platforms, and can usually be generated with Ctrl+C (though this may be configurable). It is not generated when terminal raw mode is enabled and Ctrl+C is used. -
在 Windows 上按下 Ctrl+Break 时会传递
'SIGBREAK'
。在非 Windows 平台上,它可以被监听,但无法发送或生成它。¥
'SIGBREAK'
is delivered on Windows when Ctrl+Break is pressed. On non-Windows platforms, it can be listened on, but there is no way to send or generate it. -
'SIGWINCH'
当调整控制台大小时会发送。在 Windows 上,这只会发生在当光标移动时写入控制台,或者当在原始模式下使用可读的终端时。¥
'SIGWINCH'
is delivered when the console has been resized. On Windows, this will only happen on write to the console when the cursor is being moved, or when a readable tty is used in raw mode. -
'SIGKILL'
不能安装监听器,它会无条件地终止所有平台上的 Node.js。¥
'SIGKILL'
cannot have a listener installed, it will unconditionally terminate Node.js on all platforms. -
'SIGSTOP'
不能安装监听器。¥
'SIGSTOP'
cannot have a listener installed. -
'SIGBUS'
、'SIGFPE'
、'SIGSEGV'
和'SIGILL'
,当不使用kill(2)
人为引发时,本质上会使进程处于调用 JS 监听器不安全的状态。这样做可能会导致进程停止响应。¥
'SIGBUS'
,'SIGFPE'
,'SIGSEGV'
, and'SIGILL'
, when not raised artificially usingkill(2)
, inherently leave the process in a state from which it is not safe to call JS listeners. Doing so might cause the process to stop responding. -
0
可以发送来测试进程是否存在,如果进程存在则没影响,如果进程不存在则抛出错误。¥
0
can be sent to test for the existence of a process, it has no effect if the process exists, but will throw an error if the process does not exist.
Windows 不支持信号,因此没有等价的使用信号来终止,但 Node.js 提供了一些对 process.kill()
和 subprocess.kill()
的模拟:
¥Windows does not support signals so has no equivalent to termination by signal,
but Node.js offers some emulation with process.kill()
, and
subprocess.kill()
:
-
发送
SIGINT
、SIGTERM
、和SIGKILL
会导致目标进程无条件的终止,之后子进程会报告进程被信号终止。¥Sending
SIGINT
,SIGTERM
, andSIGKILL
will cause the unconditional termination of the target process, and afterwards, subprocess will report that the process was terminated by signal. -
发送信号
0
可以作为独立于平台的方式来测试进程是否存在。¥Sending signal
0
can be used as a platform independent way to test for the existence of a process.