Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zollet53's avatar

Pythonscript wont start from Laravel controller

I have a laravel project (setup on a RHEL9 server) where i wanna run some pythonscripts and show the result on a web-blade. But i cant get the laravel project to start the script.

The code is as follow:

public function startScript($macAdress){
                $macAdress= str_replace(' ', '', $macAdress);
                echo '<body style="background-color:black">';
                        set_time_limit(3000);
                while (@ ob_end_flush()); // end all output buffers if any
                        $proc = popen('/var/www/PathOfLaravel/public/pythonscript.py ' . $macAdress, 'r');
                        echo '<pre style="color:grey;font-size: 18px;position: absolute;bottom:-15px;">';
                        while (!feof($proc))
                        {
                                echo fread($proc, 4096);
                                @ flush();
                        }
                        echo '</pre>';
                }

If i dd($proc); im getting:

stream resource @9 ▼
  timed_out: false
  blocked: true
  eof: false
  stream_type: "STDIO"
  mode: "r"
  unread_bytes: 0
  seekable: false
  options: []
}

I can run the script from terminal without any problem. And i dont know where to look for errorlogs about this. No errors is posted in storage/laravel.log or storage/error.log. Or in the php-logs.

I have also tried to start the script with:

$process = new Process(['python3', '/var/www/PathOfLaravel/public/pythonscript.py', $macAdress]);
                $process->run();
                if (!$process->isSuccessful()) {
                        throw new ProcessFailedException($process);
}
                $output_data = $process->getOutput();

But then getting errorcode that it missing modules (which it shouldn't do because it runs as it should when running from terminal). Have tried changing owner but still with the same result. This is the scriptfile that im trying to run: -rwxrwxr-x 1 apache apache 51552 Jan 22 09:16 pythonscript.py

So just now im trying to find out why the script isn't starting at all.

Thx in advance for your help ;)

0 likes
1 reply
zollet53's avatar

Found the problem now. It was missing modules/libs for python3 and user apache that started the script. So just needed to install the libs for all users that was missing, i had them installed for my user. But now the script wont flush its result. Or it dosn't flush it until the script is done. I want it to flush when for every print in the script. In the script there is alot of sys.stdout.flush() so it should flush the results. Anyone got an ide?

Please or to participate in this conversation.