process.kill(pid[, signal])


  • pid <number> 进程 ID。
  • signal <string> | <number> 要发送的信号,类型为字符串或数字。默认值: 'SIGTERM'

process.kill() 方法会发送 signalpid 标识的进程。

信号名称为字符串(比如 'SIGINT''SIGHUP')。 详见信号事件kill(2)

如果目标 pid 不存在,则该方法会抛出错误。 作为特例,信号 0 可以用于测试进程是否存在。 在 Windows 平台上,如果 pid 被用于杀死进程组,则会抛出错误。

虽然此函数的名称是 process.kill(),它其实只是信号发送器,类似于 kill 系统调用。 发送的信号可以做一些与杀死目标进程无关的事情。

process.on('SIGHUP', () => {
  console.log('收到 SIGHUP 信号');
});

setTimeout(() => {
  console.log('退出中');
  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.

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.