1
I’m trying to run a python script by php, but it doesn’t run python.
I used the code to test:
$cmdResult = shell_exec("ls & /usr/local/bin/python2.7 --version & echo done");
Returned:
done
LICENSE
example.py
When I type directly via shell:
[root@local folder]# /usr/local/bin/python2.7 --version
Python 2.7.6
Does anyone have any idea?
Additional info:
[root@local folder]# ls -all /usr/local/bin/py*
-rwxr-xr-x 1 root apache 84 Jul 21 21:53 /usr/local/bin/pydoc
lrwxrwxrwx 1 root root 24 Jul 21 21:43 /usr/local/bin/python -> /usr/local/bin/python2.7
-rwxrwxrwx 1 root apache 4669791 Jul 21 21:53 /usr/local/bin/python2.7
-rwxr-xr-x 1 root apache 1674 Jul 21 21:53 /usr/local/bin/python2.7-config
$command = escapeshellcmd('/usr/local/bin/python2.7 --version'); $cmdResult = shell_exec($command); echo $cmdResult; Did not return the Python version.
– Hoppy
@Hoppy intends to run a python script through PHP or run a shell script to know the python version through PHP?
– Rui Lima
Both do not return a result, I believe that if I can execute the command '/usr/local/bin/python2.7 --version' I can run python. I tried it the way you presented it. When I run on the shell ". /script.py or /usr/local/bin/python2.7 --version" Both work, but when PHP is running, it returns no result and does not run.
– Hoppy
Try using the absolute directory and giving all permissions to the file ". py"
– Rui Lima
$cmdResult = shell_exec("/var/www/html/example.py"); echo $cmdResult; returned nothing. as I have 2 versions of python running, to run the file I put the line "shebang" pointing to the correct version: #!/usr/local/bin/python2.7
– Hoppy
@Hoppy already found the problem. It is necessary to define the passage and not only the file as argument of the function "escapeshellcmd()". I have fixed the script.
– Rui Lima
i re-did the script in bash, I won’t have to worry about python anymore. x.x
– Hoppy