doulong1987 2015-08-31 05:24
浏览 79
已采纳

如何在php中生成一个新进程来调用需要子进程的节点脚本

I'm trying to run a PHP code in isolation. The php code simply calls a node script at intervals with required arguments. The Node.js script on the other hand computes the arguments to get additional data from mongoDb and then spawns a child process within it to call Phantomjs with those arguments and creates heatmap image file(s).

Now the Node.js script runs fine when fired from command line with the right arguments.

But no matter what I try I'm unable to get it to work when called from php.

I have tried shell_exec and exec like below:

$node = '/usr/local/bin/node';
$scriptPath = $this->basePath . '/web/scripts/node/heatmaps_node_wrapper.js';
$clickmapCmd = $node . ' ' . $scriptPath . ' ' . $url . ' ' . $groupId . ' ' . $heatmapId . ' click';
$mousemovemapCmd = $node . ' ' . $scriptPath . ' ' . $url . ' ' . $groupId . ' ' . $heatmapId . ' mousemove';
$asyncCmd = ' >' . $this->basePath . '/logs/heatmap.io.log 2>'. $this->basePath . '/logs/heatmap.err.log &';

exec($clickmapCmd . $asyncCmd);
exec($mousemovemapCmd . $asyncCmd);

also tried using symfony's ProcessBuilder Component, like so -

$builder = new ProcessBuilder();
$builder->setPrefix('/usr/local/bin/node');
$builder->setArguments(array($scriptPath, $url, $groupId, $heatmapId, 'click'));
$process = $builder->getProcess();
$process->setTimeout(1000);
$process->setIdleTimeout(1000);
$process->start();
$output = $process->getOutput();

Both fail to create the heatmap images.

But debugging the symfony code and studying symfony's Process object helped me discover the likely problem. In the objects stderr I see the following message -

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)

So my guess is the problem is with Node trying to create a child process of, of PHP's child process(??).

So my question is, what is the solution (or work around) to this problem??

Cheers!

  • 写回答

1条回答 默认 最新

  • doushenjia8514 2015-08-31 05:42
    关注

    The problem is that node can't find phantomjs. If it's not in the $PATH for the user executing the node script, you may try using the full path to the phantomjs binary. You may also want to check the executable permissions on the phantomjs binary.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog