process.kill(pid[, signal])
process.kill() 方法会向由 pid 指定的进程发送 signal。
【The process.kill() method sends the signal to the process identified by
pid.】
信号名称是诸如 'SIGINT' 或 'SIGHUP' 的字符串。有关更多信息,请参见 信号事件 和 kill(2)。
【Signal names are strings such as 'SIGINT' or 'SIGHUP'. See Signal Events
and kill(2) for more information.】
如果目标 pid 不存在,此方法将抛出错误。作为一种特殊情况,可以使用信号 0 来测试进程是否存在。如果在 Windows 平台上使用 pid 来终止进程组,将会抛出错误。
【This method will throw an error if the target pid does not exist. As a special
case, a signal of 0 can be used to test for the existence of a process.
Windows platforms will throw an error if the pid is used to kill a process
group.】
尽管这个函数的名称是 process.kill(),它实际上只是一个信号发送器,就像 kill 系统调用一样。发送的信号可能会执行除了终止目标进程之外的其他操作。
【Even though the name of this function is process.kill(), it is really just a
signal sender, like the kill system call. The signal sent may do something
other than kill the target process.】
import process, { kill } from 'node:process';
process.on('SIGHUP', () => {
console.log('Got SIGHUP signal.');
});
setTimeout(() => {
console.log('Exiting.');
process.exit(0);
}, 100);
kill(process.pid, 'SIGHUP');const process = require('node:process');
process.on('SIGHUP', () => {
console.log('Got SIGHUP signal.');
});
setTimeout(() => {
console.log('Exiting.');
process.exit(0);
}, 100);
process.kill(process.pid, 'SIGHUP');当 Node.js 进程接收到 SIGUSR1 时,Node.js 将启动调试器。详见 信号事件。
【When SIGUSR1 is received by a Node.js process, Node.js will start the
debugger. See Signal Events.】