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 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)