process.kill(pid[, signal])


  • pid <number> 进程标识
  • signal <string> | <number> 要发送的信号,可以是字符串或数字。 默认值: 'SIGTERM'

process.kill() 方法将 signal 发送到由 pid 标识的进程。

信号名称是字符串,例如 'SIGINT''SIGHUP'。 有关详细信息,请参阅信号事件kill(2)

如果目标 pid 不存在,则此方法将抛出错误。 作为特殊情况,可以使用信号 0 来测试进程是否存在。 如果使用 pid 来杀死进程组,则 Windows 平台将抛出错误。

尽管此函数的名字是 process.kill(),但它实际上只是信号发送者,就像 kill 系统调用。 发送的信号可能会做其他事情而不是杀死目标进程。

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 将启动调试器。 参见信号事件

  • pid <number> A process ID
  • signal <string> | <number> The signal to send, either as a string or number. Default: 'SIGTERM'.

The process.kill() method sends the signal to the process identified by pid.

Signal names are strings such as 'SIGINT' or 'SIGHUP'. See Signal Events and kill(2) for more information.

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.

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');

When SIGUSR1 is received by a Node.js process, Node.js will start the debugger. See Signal Events.