title: Killing a FFmpeg process triggered by spawn in NodeJs
body: <p>I am facing an issue wanting to kill a <code>FFmpeg</code> process triggered by <code>spawn</code> from the <code>child_process</code> native <code>NodeJs</code> package.</p>
  <p>Here is the script i use to trigger the ffmpeg process.</p>
  <p>Assume that the conversion takes a long time, like about 2 hours.</p>
  <pre><code>/**
   * Execute a command line cmd
   * with the arguments in options given as an array of string
   * processStore is an array that will store the process during its execution
   * and remove it at the end of the command or when error occurs
   */
  function execCommandLine({
     cmd,
     options = [],
     processStore = [],
  }) {
    return new Promise((resolve, reject) =&gt; {
      const spwaned_process = childProcess.spawn(cmd, options);
  
      // Append the process to a buffer to keep track on it
      processStore.push(spwaned_process);
  
      // Do nothing about stdout
      spwaned_process.stdout.on('data', () =&gt; true);
  
      // Do nothing about stderr
      spwaned_process.stderr.on('data', () =&gt; true);
  
      spwaned_process.on('close', () =&gt; {
        const index = processStore.indexOf(spwaned_process);
  
        if (index !== -1) {
          processStore.splice(index, 1);
        }
  
        resolve();
      });
  
      spwaned_process.on('error', () =&gt; {
        const index = processStore.indexOf(spwaned_process);
  
        if (index !== -1) {
          processStore.splice(index, 1);
        }
  
        reject();
      });
    });
  }
  
  const processStore = [];
  
  await execCommandLine({
    cmd: 'ffmpeg',
  
    options: [
      '-i',
      '/path/to/input',
      '-c:v',
      'libvpx-vp9',
      '-strict',
      '-2',
      '-crf',
      '30',
      '-b:v',
      '0',
      '-vf',
      'scale=1920:1080',
      '/path/to/output',
    ],
  
    processStore,
  });
  </code></pre>
  <p>During the conversion, the following code is called to kill all process into the <code>processStore</code>, including the <code>FFmpeg</code> process that is triggered.</p>
  <pre><code>// getProcessStore() returns the const processStore array of the above script
  getProcessStore().forEach(x =&gt; x.kill());
  
  process.exit();
  </code></pre>
  <p>After the program exit, when i run <code>ps -ef | grep ffmpeg</code>, there is still some <code>FFmpeg</code> process running.</p>
  <blockquote>
  <p>root       198     1  0 09:26 ?        00:00:00 ffmpeg -i /path/to/input -ss 00:01:47 -vframes 1 /path/to/output</p>
  <p>root       217     1  0 09:26 ?        00:00:00 ps -ef</p>
  </blockquote>
  <p>Do you have an idea about the way to kill a ffmpeg process properly ?</p>