I have a python script writetest.py with the following:
print "mark1"
with open("/testfile.txt", "a") as myfile:
myfile.write("Hello World")
print "mark2"
When I call the scrip in the shell, it work and adds "Hello World" the testfile.txt. When I call it using the php exec command, it doesn't write:
exec('python writetest.py', $output, $return_var);
print_r($output);
print_r($return_var);
Which prints:
Array ( [0] => mark1 )
1
My python script runs but stops at the open code.
I think there is quote issue in your code
exec('python writetest.py")
Once you fix that, try again.
If still not working I would suggest
exec('python writetest.py', $output , $return_var)
see: http://php.net/manual/en/function.exec.php
Then print output and return_var by print_r for debugging purpose
print_r($output);
print_r($return_var);
As aynber pointed out, it was a permissions issue. I gave the web server user write access and it worked
Related
I am trying to run a Python script inside PHP and show the output.
I tried with simple test.py inside PHP, which print hello world without problem. But when I try to execute my desired command from PHP script the exec() returns empty string instead of output.
So the here is my php script. Below snippets works fine:
$output = exec("python test.py");
var_dump($output);
But not the desired one.
$command = "python -m scripts.label_image --graph=tf_files/retrained_graph.pb --image=".$uploadfile;
$output = exec($command);
var_dump($output);
so the folder structure is:
/var/www/mysite/
-scripts(folder which contains python scripts)
-tf_files(folder with other files)
-uploads (image folder)
Can someone tell me what is going wrong?
Sample output in stdout/shell:
the variable $output should an argument.
If you do this, you should get only the last output of your script.
exec("python test.py", $output);
var_dump($output);
https://www.php.net/manual/en/function.exec.php
I am simply calling python script from my php program and I need to display the output return from python script to webpage. I am new to this, so I tried many ways to get the output return,
1.
$command = escapeshellcmd('python script.py "ab" "cd" ');
$output = shell_exec($command);
2.
$output = shell_exec('python script.py "ab" "cd" ');
print $output;
3.
$command = system('python script.py "ab" "cd" ',$output);
echo "output is ".$output;
echo "command is".$command;
4.
$command = passthru ('python script.py "ab" "cd" ');
$output = shell_exec($command);
echo $output;
5.
exec('python script.py "ab" "cd" ', $output, $return_var);
echo $output;
echo $return_var;
If time taken taken by python script is less, then I able to get the output return to my php program and able to display on my web page.
print "Hello"
print "world"
But when my python program is calling any background process, which taking more time and giving output after few seconds, then that parts are not returning to the php program.
print "Hello"
returned_value=subprocess.check_output('ssh -p 29418 xyz.com --format=JSON', shell=True)
print returned_value
I tried to include sleep in php program but it is not helping as output is already returned. Some posts suggested to use Ajax Whether that is the only and correct way to solve this question?
Thanks in advance for your help or suggestion.
Php never get response If I use sleep for 5 minutes or more in python. Using sleep around 2 minutes , its working, I do not know what is happening, where is the problem?
Sample Code
uploadfile_database.php:
<?php
$file_id=1;
$response=exec('python /home/xyz/test.py '.$file_id);
echo $response;
?>
test.py
import os, json
import sys
sleep(300)
#sleep(420)
print "hello"
exec does not work as you're expecting. To get the output from of your python script you need to pass an output parameter to exec.
Ex.
$output = array();
$exit_code = exec('python /home/xyz/test.py '.$file_id, &$output);
You could also just use shell_exec which would work as you're expecting
I am running this test code using exec() in PHP
exec('#!/bin/bash');
exec('abc=10');
echo exec('echo $abc'); // no output
echo exec('whoami'); // this works fine
If I run the first 3 lines in terminal, the output is '10'.
But PHP does not output anything. What am I doing wrong?
http://php.net/manual/en/function.exec.php
Manual says use output_arr to capture output.
<?php
Exec ($cmd, $output_arr);
Print_r ( $output_arr);
?>
#zarathuztra is right. Try combining the various commands in a single exec with ; in between. Also check my answer if output_arr gets what you want.
I want to see the output of the following code in the web browser:
code:
<?php
$var = system('fdisk -l');
echo "$var";
?>
When I open this from a web browser there is no output in the web browser. So how can I do this? Please help!
Thanks
Puspa
you can use passthru, like so:
$somevar = passthru('echo "Testing1"');
// $somevar now == "Testing1"
or
echo passthru('echo "Testing2"');
// outputs "Testing2"
use exec('command', $output);
print_r($output);
First of all, be sure that you (=user under which php runs) are allowed to call the external program (OS access rights, safe_mode setting in php.ini). Then you have quite a few options in PHP to call programs via command line. The most common I use are:
system
This function returns false if the command failed or the last line of the returned output.
$lastLine = system('...');
shell_exec or backtick operators
This function/operator return the whole output of the command as a string.
$output = shell_exec('...'); // or:
$output = `...`;
exec
This function returns the last line of the output of the command. But you can give it a second argument that then contains all lines from the command output.
$lastLine = exec('...'); // or capturing all lines from output:
$lastLine = exec('...', $allLines);
Here is the overview of all functions for these usecases: http://de.php.net/manual/en/ref.exec.php