util.convertProcessSignalToExitCode(signal)


  • signal <string> 信号名称(例如 'SIGTERM'
  • 返回:<number> signal 对应的退出代码

util.convertProcessSignalToExitCode() 方法将信号名称转换为相应的 POSIX 退出代码。根据 POSIX 标准,由信号终止的进程的退出代码计算方式为 128 + signal number

🌐 The util.convertProcessSignalToExitCode() method converts a signal name to its corresponding POSIX exit code. Following the POSIX standard, the exit code for a process terminated by a signal is calculated as 128 + signal number.

如果 signal 不是有效的信号名称,将会抛出错误。有关有效信号的列表,请参见 signal(7)

🌐 If signal is not a valid signal name, then an error will be thrown. See signal(7) for a list of valid signals.

import { convertProcessSignalToExitCode } from 'node:util';

console.log(convertProcessSignalToExitCode('SIGTERM')); // 143 (128 + 15)
console.log(convertProcessSignalToExitCode('SIGKILL')); // 137 (128 + 9)const { convertProcessSignalToExitCode } = require('node:util');

console.log(convertProcessSignalToExitCode('SIGTERM')); // 143 (128 + 15)
console.log(convertProcessSignalToExitCode('SIGKILL')); // 137 (128 + 9)

当处理进程时,根据终止进程的信号确定退出代码,这尤其有用。

🌐 This is particularly useful when working with processes to determine the exit code based on the signal that terminated the process.