title: node.js spawn. exit event fired straight away
body: <p>I am using the spawn function with mjpg_streamer. However the child process just exits as soon as it is created. Here is the code I am using</p>
  
  <pre><code>streamingProcess = spawn('mjpg_streamer', [
      '-i', '"/usr/local/lib/input_file.so -f ' + IMAGE_PATH + ' -n ' + IMAGE_NAME + '"',
      '-o', '"/usr/local/lib/output_http.so -w /usr/local/www"'
  ]);
  
  streamingProcess.on('exit', function(code, signal) {
      console.log('streaming process has stopped. Code: ' + code + ', signal: ' + signal);
      streamingProcess = null;
  });
  </code></pre>
  
  <p>running the actual command itself returns the following:</p>
  
  <p><img src="https://i.stack.imgur.com/uXm6M.jpg" alt="enter image description here"></p>
  
  <p>What the mjpg_streamer command essentially does is fire up a webserver. The code in the exit callback is 1, and the signal is <code>null</code>.</p>
  
  <p>Not quite sure what is going on...</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>If I use <code>exec</code>, then it works fine! However, the process will not die if I call kill! </p>
  
  <p>So </p>
  
  <pre><code> streamingProcess = exec('LD_LIBRARY_PATH=/usr/local/lib mjpg_streamer -i "input_file.so -f /tmp/stream -n pic.jpg" -o "output_http.so -w /usr/local/www"');
  </code></pre>
  
  <p>does indeed fire up the webserver, but now if I call</p>
  
  <pre><code> streamingProcess.kill()
  </code></pre>
  
  <p>the exit event is fired, but the process itself does not die. On further inspection it seems as if the pid's (from the node child process and on the os) are not the same??</p>
  
  <p>Now I am confused...</p>
  
  <p><strong>UPDATE 2:</strong></p>
  
  <p>Ok I think I am starting to understand what is going on with <code>exec</code>. So it executes the command in a subshell, which for me is creating two processes. One is the subshell, and the other is the mjpg_streamer command itself. So killing the <code>exec</code> only kills the subshell. But the mjpg_streamer remains.</p>
  
  <p>From node:</p>
  
  <p><img src="https://i.stack.imgur.com/nsZki.jpg" alt="enter image description here"></p>
  
  <p>From the os:</p>
  
  <p><img src="https://i.stack.imgur.com/3LW4z.jpg" alt="enter image description here"></p>
  
  <p>So 3752 is the actual command I want to kill. I suppose there is no way for me to get this pid from node? :)</p>