I've seen several posts about this subject, none worked. exec() and similar shell commands works fine only for Linux commands (eg. ls) not for Python scripts Initially I thought there was something wrong with my test.py, but even a simple print() doesn't work. Here's what happen:
OS: Ubuntu 22.04
PHP: 8.0
Python: 3.10
test.py
#!/usr/bin/env python
Print("Hello Word") # that simple
from terminal, it works:
> python3 test.py // Hello World
myscript1.php | doesn't work
$command = escapeshellcmd("/mysite.com/path/python3 test.py ");
$out = shell_exec($command);
file_put_contents( $txt_path.'test.txt', $out); // test.txt is empty
myscript2.php | it works, but it's not a python script
$output = shell_exec('ls -lart');
$t1= "<pre>$output</pre>";
file_put_contents( $txt_path.'out.txt', $t1 ) ; // out.txt contains the list of files
I've granted permissions to all users, and test.py is executable. Tried all shell functions: passthru(), shell_exec(), exec() php.ini -> disabled_functions: none of them is listed, and I've removed pcntl_exec()
> sudo chmod a+rwx /mysite.com/path/
> chmod +x test.py
Please help Many thanks