title: How to spawn a new process in php to call a node script that requires a child process
body: <p>I'm trying to run a <strong>PHP</strong> code in isolation. The php code simply calls a node script at intervals with required arguments. The <strong>Node.js</strong> script on the other hand computes the arguments to get additional data from <strong>mongoDb</strong> and then spawns a child process within it to call <strong>Phantomjs</strong> with those arguments and creates heatmap image file(s). </p>
  
  <p>Now the Node.js script runs fine when fired from command line with the right arguments.</p>
  
  <p>But no matter what I try I'm unable to get it to work when called from php.</p>
  
  <p>I have tried shell_exec and exec like below:</p>
  
  <pre><code>$node = '/usr/local/bin/node';
  $scriptPath = $this-&gt;basePath . '/web/scripts/node/heatmaps_node_wrapper.js';
  $clickmapCmd = $node . ' ' . $scriptPath . ' ' . $url . ' ' . $groupId . ' ' . $heatmapId . ' click';
  $mousemovemapCmd = $node . ' ' . $scriptPath . ' ' . $url . ' ' . $groupId . ' ' . $heatmapId . ' mousemove';
  $asyncCmd = ' &gt;' . $this-&gt;basePath . '/logs/heatmap.io.log 2&gt;'. $this-&gt;basePath . '/logs/heatmap.err.log &amp;';
  
  exec($clickmapCmd . $asyncCmd);
  exec($mousemovemapCmd . $asyncCmd);
  </code></pre>
  
  <p>also tried using symfony's ProcessBuilder Component, like so - </p>
  
  <pre><code>$builder = new ProcessBuilder();
  $builder-&gt;setPrefix('/usr/local/bin/node');
  $builder-&gt;setArguments(array($scriptPath, $url, $groupId, $heatmapId, 'click'));
  $process = $builder-&gt;getProcess();
  $process-&gt;setTimeout(1000);
  $process-&gt;setIdleTimeout(1000);
  $process-&gt;start();
  $output = $process-&gt;getOutput();
  </code></pre>
  
  <p>Both fail to create the heatmap images.</p>
  
  <p>But debugging the symfony code and studying symfony's <strong>Process</strong> object helped me discover the likely problem. In the objects stderr I see the following message -</p>
  
  <pre><code>events.js:85
    throw er; // Unhandled 'error' event
          ^
  Error: spawn phantomjs ENOENT
  at exports._errnoException (util.js:746:11)
  at Process.ChildProcess._handle.onexit (child_process.js:1053:32)
  at child_process.js:1144:20
  at process._tickCallback (node.js:355:11)
  </code></pre>
  
  <p>So my guess is the problem is with Node trying to create a child process of, of PHP's child process(??).</p>
  
  <p>So my question is, what is the solution (or work around) to this problem??</p>
  
  <p>Cheers!</p>