skip to Main Content

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

SymfonyComponentProcessExceptionProcessSignaledException
The process has been signaled with signal "4".

SymfonyComponentProcessProcess::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;

}

2

Answers


  1. Chosen as BEST ANSWER

    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


  2. 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);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search