简体   繁体   中英

shell_exec of a PhantomJS script kills PHP-FPM and Apache

I am having a very strange issue with a 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.

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:

$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. I need to add that I am executing this command asynchronously via AJAX call.

I've tried different approaches - using something else instead of shell_exec , making a Bash script that will accept arguments, PhantomJS Runner.... nothing works.

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?

I recently published a project that gives PHP access to a browser. Get it here: https://github.com/merlinthemagic/MTS , Under the hood is an instance of PhantomJS just like you are trying to do.

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. (even running 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. Also, there was no problem if I execute from command-line.

How did you install phantomjs?

Grabbing binary package from http://phantomjs.org/download.html instead of npm -g install phantomjs solved this problem for me.

Hope this helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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