title: Spawn and kill a process in node.js
body: <p>I'm trying to spawn a process in javascript, and kill it after some time (for testing purposes).</p>
  
  <p>In the end, the process will be a infinite loop that I need to restart with different arguments at specified time, so I thought that spawning the process and killing it was the best way to do this.</p>
  
  <p>My test code is:</p>
  
  <pre><code>var spawn=require('child_process').spawn
  , child=null;
  
  child=spawn('omxplayer', ['test.mp4'], function(){console.log('end');}, {timeout:6000});
  console.log('Timeout');
  setTimeout(function(){
      console.log('kill');
      child.kill();
  }, 1200);
  
  child.stdout.on('data', function(data){
      console.log('stdout:'+data);
  });
  
  child.stderr.on('data', function(data){
      console.log('stderr:'+data);
  });
  
  child.stdin.on('data', function(data){
      console.log('stdin:'+data);
  });
  </code></pre>
  
  <p>The result is:</p>
  
  <pre><code>#~$ node test.js
  Timeout
  kill
  </code></pre>
  
  <p>But I still need to send ctrl+C to end the program. What am I missing?</p>
  
  <p>On Raspbian, node 0.10.17, omxplayer is a binary (video player).</p>
  
  <p>I tried:</p>
  
  <ul>
  <li>Added <code>chmod +x</code> to the app.</li>
  <li>Launched as root.</li>
  <li>Paused stdin of the child process. Using all terminate-related signal in the kill command.</li>
  </ul>
  
  <p>I also launched a <code>ps</code> command while the app was running:</p>
  
  <pre><code>2145    bash
  2174    node
  2175    omxplayer
  2176    omxplayer.bin
  2177    ps
  </code></pre>
  
  <p>So omxplayer is a wrapper, who don t kill it's child process when it end, is there any way to get the pid of the wrapped process?</p>
  
  <p>Still biting dust, tried this:</p>
  
  <pre><code>spawn('kill', ['-QUIT', '-$(ps opgid= '+child.pid+')']);
  </code></pre>
  
  <p>Which I thought would kill all children of omxplayer, I don t know if using spawn like that is wrong or if it's the code that doesn't work.</p>
  
  <p>The last edit I made was the good answer, but had to be edited a bit.</p>
  
  <p>I created a sh file (with execute right) like this:</p>
  
  <pre><code>PID=$1
  PGID=$(ps opgid= "$PID")
  kill -QUIT -"$PGID"
  </code></pre>
  
  <p>Which I start like this:</p>
  
  <pre><code>execF('kill.sh', [child.pid], function(){
      console.log('killed');
  });
  </code></pre>
  
  <p>Instead of <code>child.kill</code>.</p>
  
  <p>I'm not sure if it s the best way to do, nor if the code is clean, but it does work.</p>
  
  <p>I'll accept any answer which make it in a cleaner way or, even better, without having to execute a file.</p>