PHP shell_exec is not responding on python command line - php

Hi I am running shell_exec on my PHP Application,
Here is the code,
$path = "C:/scripts/";
chdir($path);
$py_commonscript = 'python Common_Script.py';
$exec = shell_exec($py_commonscript);
echo "<pre>$exec</pre>";
This code doesn't give any result and does not generate a file.
When I tried running manually on the command line it is working and was able to generate a file.
I tried to execute this one below and it was able to display some result.
$sample = shell_exec('ls -lart');
echo "<pre>$sample </pre>";
I am wondering why the command for $exec is not being triggered or run.

The most likely cause is that your PHP code isn't inheriting a %PATH containing python.exe. Try print getenv('PATH'); or just use the full path to python.exe in your command line.

Related

Unable to execute Python script from PHP shell_exec

I'm trying to execute a python (Python 3) script from the PHP shell_execfunction but it doesn't seem to be working, I'm trying it on a windows system but it will be in CentOS 7 in production.
In the below code snippet nothing happens when the PHP runs. But it I use the $response = shell_exec("php -v"); line it displays the PHP version ok. I've also tried running the python script directly on the command line and it runs fine with no errors.
Code
$command = "\"C:\Programs\Python\Python36-32\python.exe\" \"E:\\htdocs\\dev\\_nodes\\jobs\\jobCheck.py\" \"https:/www.google.ie\" 1";
$response = shell_exec($command);
//$response = shell_exec("php -v");
echo $response;
Command Line
"C:\Programs\Python\Python36-32\python.exe" "E:\\htdocs\\dev\\_nodes\\jobs\\jobCheck.py" "https:/www.google.ie" 1
Update #1
I've gotten the python script to run via PHP, but the python script crashes once it hits driver = webdriver.PhantomJS(), the import exists in the python script from selenium import webdriver and all this functionality works fine once called directly via the command line but doesn't seem to work once the python script is called from PHP.
Here's a sample script that prints out test 1 but not test 2 when called from the PHP script.
from selenium import webdriver
print("test 1")
driver = webdriver.PhantomJS()
print("test 2")
driver.quit()
Update #2
Looks like it was an issue with paths, there is a file created in the python script that was using a relative path, once I changed the path to be a full path the script ran ok both in the command line and when being called via the PHP script.
You need to scape the command string.
Try using escapeshellarg function.
$command = escapeshellarg('C:\Programs\Python\Python36-32\python.exe "E:\htdocs\dev\_nodes\jobs/jobCheck.py" "https:/www.google.ie" 1');
$response = shell_exec($command);
//$response = shell_exec("php -v");
echo $response;
http://php.net/manual/pt_BR/function.escapeshellarg.php

Execution of a Python Script from PHP

I am making an android application in which I am first uploading the image to the server and on the server side, I want to execute a Python script from PHP. But I am not getting any output. When I access the Python script from the command prompt and run python TestCode.py it runs successfully and gives the desired output. I'm running Python script from PHP using the following command:
$result = exec('/usr/bin/python /var/www/html/Source/TestCode.py');
echo $result
However, if I run a simple Python program from PHP it works.
PHP has the permissions to access and execute the file.
Is there something which I am missing here?
exec('/usr/bin/python /var/www/html/Source/TestCode.py', $output);
var_dump($output);
2nd Parameter of exec will give output
EDIT:
exec('/usr/bin/python /var/www/html/Source/TestCode.py 2>&1', $output);
2>&1 - redirecting stderr to stdout. Now in case of any error too, $output will be populated.
First Check your python PATH using "which python" command and check result is /usr/bin/python.
Check your "TestCode.py" if you have written #!/usr/bin/sh than replace it with #!/usr/bin/bash.
Than run these commands
exec('/usr/bin/python /var/www/html/Source/TestCode.py', $result);
echo $result

Using Php to execute command line

I got some problem while using php to execute command line.
I have a software and I need to execute the software to export pdf by using cmd
command line.
(The software need to be executed by using command-line)
Therefore i wrote a php code.
<?php
$cmd ='C:\XmlServer.exe C:\input.xml';
shell_exec($cmd);
?>
I've tried the string 'C:\XmlServer.exe C:\input.xml' is working on cmd.
But I can't using php to execute command line to get the same result.
I also tried exec($cmd);, but it still didn't work.
Could anyone help me solving the problem?
I want to run php just like running command line.
I wrote like the following in command line:
C:\> XmlServer.exe input.xml
then it's ok to output a file for me.
but using the same code in php didn't work.
--
Update
using echo shell_exec($cmd); is okay, but no output.
try with echo like that :
<?php
$cmd ='C:\XmlServer.exe input.xml';
echo exec($cmd);
?>

Running a python code with php

I have a python script which is needed to be run by php:
<?php
$command = escapeshellcmd('/home/Desktop/test.py');
$output = shell_exec($command);
echo $output;
?>
The out put of the python script is a binary file but I get the following error in the log:
Unable to access the X Display, is $DISPLAY set properly?
The php code works fine from the terminal but no luck when I try to run it from the browser. Any idea what is going on? Ideally, I don't want to change my program. I want to know how you can rectify the X Display error. Is there a way to check if $DISPLAY is set properly? ( I am working with Ubuntu)
I tried this : pidof X && echo "yup X server is running" on my terminal and it is saying yup x server is running!
Add the following text as the first line of your Python script:
#!/usr/bin/python
Without this, the kernel doesn't know what interpreter to run your script with, and may end up trying to launch it using /usr/bin/import (because that word probably appears on the first line of the script). The import utility requires access to the X11 display (it's a screenshot utility, basically), which is why you're getting this error.
The python file you need may need to open a window to run. You say you saw it run in terminal though? What's test.py? Is it propitiatory?
If you try using this as a command in your PHP: (not 100% that the shell escape won't strip this so may need to comment that out)
python -c \'print \"Test\"\'
and see if you get the output text back. If so it's a python issue not PHP and the test.py file may be instantiating something that needs the $DISPLAY var set. PHP doesn't set the $DISPLAY var as it is shell commands not GUI.
try popen
$command = "/usr/bin/python /home/Desktop/test.py";
$handle = popen($command, "r");
"r" for read
$read = fread($handle, 10);
10 is the size of output you want to take,
echo $read ;
hope it helps,

php exec returns less results than entering into command line directly

I have an exec command that is behaving differently than the same command given to linux through Penguinet.
$res = exec('cd /mnt/mydirectory/; zcat log_file.gz');
echo $res
When putting the commands directly into the command line, I see about 100 entries in the log file. However when I access the PHP page that has the exec() command, I see only 1. And it is formatted correctly. Why does PHP show me only one result? How can I make it show the entire contents of the file?
EDIT:
Seems this is only returning only the last line. How can I change that?
try this:
exec('cd /mnt/mydirectory/; zcat log_file.gz', $res);
print_r($res);

Categories