subprocess.kill([signal])


subprocess.kill() 方法会向子进程发送信号。如果没有提供参数,进程将被发送 'SIGTERM' 信号。有关可用信号的列表,请参见 signal(7)。如果 kill(2) 成功,该函数返回 true,否则返回 false

【The subprocess.kill() method sends a signal to the child process. If no argument is given, the process will be sent the 'SIGTERM' signal. See signal(7) for a list of available signals. This function returns true if kill(2) succeeds, and false otherwise.】

const { spawn } = require('node:child_process');
const grep = spawn('grep', ['ssh']);

grep.on('close', (code, signal) => {
  console.log(
    `child process terminated due to receipt of signal ${signal}`);
});

// Send SIGHUP to process.
grep.kill('SIGHUP');import { spawn } from 'node:child_process';
const grep = spawn('grep', ['ssh']);

grep.on('close', (code, signal) => {
  console.log(
    `child process terminated due to receipt of signal ${signal}`);
});

// Send SIGHUP to process.
grep.kill('SIGHUP');

如果信号无法传递,ChildProcess 对象可能会发出 'error' 事件。向已经退出的子进程发送信号并不是错误,但可能会产生意想不到的后果。特别是,如果进程标识符 (PID) 已被分配给其他进程,信号将会发送给该进程,从而可能产生意外的结果。

【The ChildProcess object may emit an 'error' event if the signal cannot be delivered. Sending a signal to a child process that has already exited is not an error but may have unforeseen consequences. Specifically, if the process identifier (PID) has been reassigned to another process, the signal will be delivered to that process instead which can have unexpected results.】

虽然该函数名为 kill,但发送给子进程的信号可能实际上并不会终止该进程。

【While the function is called kill, the signal delivered to the child process may not actually terminate the process.】

请参阅 kill(2)以供参考。

【See kill(2) for reference.】

在 Windows 上,由于 POSIX 信号不存在,signal 参数将被忽略,除了 'SIGKILL''SIGTERM''SIGINT''SIGQUIT',进程总是会被强制且突然终止(类似于 'SIGKILL')。更多详情请参见 信号事件

【On Windows, where POSIX signals do not exist, the signal argument will be ignored except for 'SIGKILL', 'SIGTERM', 'SIGINT' and 'SIGQUIT', and the process will always be killed forcefully and abruptly (similar to 'SIGKILL'). See Signal Events for more details.】

在 Linux 上,当尝试终止父进程时,子进程的子进程不会被终止。这种情况很可能在通过 shell 运行新进程或使用 ChildProcessshell 选项时发生:

【On Linux, child processes of child processes will not be terminated when attempting to kill their parent. This is likely to happen when running a new process in a shell or with the use of the shell option of ChildProcess:】

const { spawn } = require('node:child_process');

const subprocess = spawn(
  'sh',
  [
    '-c',
    `node -e "setInterval(() => {
      console.log(process.pid, 'is alive')
    }, 500);"`,
  ], {
    stdio: ['inherit', 'inherit', 'inherit'],
  },
);

setTimeout(() => {
  subprocess.kill(); // Does not terminate the Node.js process in the shell.
}, 2000);import { spawn } from 'node:child_process';

const subprocess = spawn(
  'sh',
  [
    '-c',
    `node -e "setInterval(() => {
      console.log(process.pid, 'is alive')
    }, 500);"`,
  ], {
    stdio: ['inherit', 'inherit', 'inherit'],
  },
);

setTimeout(() => {
  subprocess.kill(); // Does not terminate the Node.js process in the shell.
}, 2000);