简体   繁体   English

PhantomJS脚本的shell_exec杀死了PHP-FPM和Apache

[英]shell_exec of a PhantomJS script kills PHP-FPM and Apache

I am having a very strange issue with a shell_exec . 我在shell_exec时遇到了一个非常奇怪的问题。 What I am trying to do is to call a PhantomJS and pass 4 arguments to it (user Session ID, URL, file save name and element that I need to take) using shell_exec in order to make a screenshot of a certain element on a page. 我想要做的是调用一个PhantomJS并使用shell_exec传递4个参数(用户会话ID,URL,文件保存名称和我需要使用的元素),以便在页面上制作某个元素的屏幕截图。

So, my command looks like this: 因此,我的命令如下所示:

/usr/local/bin/phantomjs  --ignore-ssl-errors=true  /Users/Igor/Sites/something/public_html/lib/phantomjs/script.js https://testsite.com/dashboard/\?dashboard_view=somedash /Users/Igor/Sites/something/uploads/screenshots/j78O3LstYumBnUi fef81e774e845d44044c167c6847f4d1 chart1 2>&1

This is what I execute using shell_exec: 这是我使用shell_exec执行的内容:

$command = ' --ignore-ssl-errors=true ' . $this->script . ' ' . escapeshellarg($this->uri) . ' ' . escapeshellarg($path) . ' ' . $this->session . ' ' . $this->element;
$output = shell_exec("$this->phantomjs $command 2>&1 1> /dev/null");

After I call shell_exec I clearly see that command has been executed using "ps uax | grep phantom" but it stays there forever, causing a instant death of PHP-FPM (v5.4.45) and Apache2 on a server. 调用shell_exec我清楚地看到该命令已使用"ps uax | grep phantom"但它一直存在,从而导致服务器上的PHP-FPM(v5.4.45)和Apache2立即死亡。 I need to add that I am executing this command asynchronously via AJAX call. 我需要补充一点,我正在通过AJAX调用异步执行此命令。

I've tried different approaches - using something else instead of shell_exec , making a Bash script that will accept arguments, PhantomJS Runner.... nothing works. 我尝试了不同的方法-使用其他方法代替shell_exec ,制作一个可以接受参数的Bash脚本PhantomJS Runner...。

If I execute this command in a console - it works without issues. 如果我在控制台中执行此命令-它可以正常工作。

I am out of ideas really - not sure how to proceed any further so any help would be appreciated. 我真的没有主意-不确定如何进行进一步的操作,因此将不胜感激。

Maybe it's a file permissions issue? 也许是文件权限问题? What are the permissions of /Users/Igor/Sites/something/public_html/lib/phantomjs/script.js? /Users/Igor/Sites/something/public_html/lib/phantomjs/script.js的权限是什么?

I recently published a project that gives PHP access to a browser. 我最近发布了一个项目,该项目使PHP可以访问浏览器。 Get it here: https://github.com/merlinthemagic/MTS , Under the hood is an instance of PhantomJS just like you are trying to do. 在这里获取它: https : //github.com/merlinthemagic/MTS ,就像您正在尝试做的一样,在其内部是PhantomJS的实例。

After downloading and setup you would simply use the following code: 下载和设置后,您只需使用以下代码:

$myUrl          = "https://testsite.com/dashboard/?dashboard_view=somedash";
$windowObj      = \MTS\Factories::getDevices()->getLocalHost()->getBrowser('phantomjs')->getNewWindow($myUrl);

//find the dimensions of the element, i **ASSUME** the id of the element is 'chart1', if not enter the correct value here
$selector   = '[id=chart1]';
$element    = $windowObj->getElement($selector);
$loc         = $element['location'];

//tell the screenshot to only get the part you want
$windowObj->setRasterSize($loc['top'], $loc['left'], ($loc['right'] - $loc['left']), ($loc['bottom'] - $loc['top']));

$imageData  = $windowObj->screenshot("png");

//save your image
file_put_contents("/path/where/you/wanna/save.png", $imageData);

I've got similar issue with PHP7.0 + php-fpm + nginx. 我在PHP7.0 + php-fpm + nginx中也遇到了类似的问题。 (even running exec('phantomjs -v'); ) (甚至正在运行exec('phantomjs -v');

In my case, I've got endless warning message (which trying to restart child process) in php7.0-fpm.log after running command through nginx. 就我而言,通过nginx运行命令后,我在php7.0-fpm.log中收到了无尽的警告消息(试图重新启动子进程)。 Also, there was no problem if I execute from command-line. 另外,如果我从命令行执行也没有问题。

How did you install phantomjs? 您是如何安装phantomjs的?

Grabbing binary package from http://phantomjs.org/download.html instead of npm -g install phantomjs solved this problem for me. http://phantomjs.org/download.html而不是npm -g install phantomjs二进制包, npm -g install phantomjs为我解决了这个问题。

Hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM