0

I'm trying to take a screenshot of a website using laravel and headless chrome. Google Chrome was installed successfully on my Centos 7 server. I'm doing test using this shell command and it was successful.

    google-chrome --headless --screenshot --window-size=1350,768 --hide-scrollbars https://google.com

In Laravel 6.6.0, I'm using Symfony Process to run the command. Below is the code.

    $process = new Process("google-chrome --headless --screenshot --window-size=1350,768 --hide-scrollbars https://google.com");
    $process->setTimeout(8000);
    $process->setWorkingDirectory(storage_path('app/public/'));
    $process->run();

However, it gives below error. I don't know what error it is.

I tried to verify the server permission by replacing with this code. It was successful.

    $process = new Process('cat > test2.txt');
    $process->setWorkingDirectory(storage_path('app/public'));
    $process->run();

Also tried oh plain php with this code

<?php

   shell_exec('google-chrome --headless --screenshot --window-size=1350,768 --hide-scrollbars --no-sandbox https://google.com'); //failed

   shell_exec('cat > test.txt'); //success

?>

Am I missing something?

Below is Laravel Error

Symfony\Component\Process\Exception\ProcessSignaledException
The process has been signaled with signal "4".

Symfony\Component\Process\Process::wait  :426
vendor/symfony/process/Process.php:426

.

$this->callback = $this->buildCallback($callback);

    }



    do {

        $this->checkTimeout();

        $running = '\\' === \DIRECTORY_SEPARATOR ? $this->isRunning() : $this->processPipes->areOpen();

        $this->readPipes($running, '\\' !== \DIRECTORY_SEPARATOR || !$running);

    } while ($running);



    while ($this->isRunning()) {

        $this->checkTimeout();

        usleep(1000);

    }



    if ($this->processInformation['signaled'] && $this->processInformation['termsig'] !== $this->latestSignal) {

        throw new ProcessSignaledException($this);

    }



    return $this->exitcode;

}
luca ditrimma
  • 765
  • 1
  • 7
  • 22

2 Answers2

0

Signal 4 is related to Permission issue, make your www-data user a sudoer on the command "google-chrome" and change your command to sudo google-chrome --head ...

Also, your code

$url = "https://google.com";
$name = Str::slug($url, '-');

turns https://google.com into httpsgooglecom. I think your issue is with the variable $name being not a valid url.

Use $url:

$process = new Process("sudo google-chrome --headless --screenshot --window-size=1350,768 --hide-scrollbars --virtual-time-budget=250005" .$url);
N69S
  • 16,110
  • 3
  • 22
  • 36
  • same error. I even put the url directly in the command without the variable. Fyi, I have posted full stack trace in the question. – luca ditrimma Dec 03 '19 at 08:19
  • @lucaditrimma Edited the answer and provided a temporary solution with sudo – N69S Dec 03 '19 at 15:49
  • yes you are right. It's SELinux permission issue but sudo didn't work either. I have to turn off the SELinux. However, this suggestion didn't work for me https://stackoverflow.com/questions/37257975/permissions-issue-with-laravel-on-centos – luca ditrimma Dec 04 '19 at 00:30
0

I found out SELinux that causing the problem. After turning it off, the code successfully do the screenshot and write to that folder. I found this solution on how to do it with SELinux enabled but it not worked for me right now. Maybe it solves others.

Permissions Issue with Laravel on CentOS

luca ditrimma
  • 765
  • 1
  • 7
  • 22