I am trying to call a simple python script
#!/usr/local/python25/bin/python
print "hello world"
from the following php script
<?php
echo exec("/usr/local/python25/bin/python myfile.py");
?>
But nothing was happened.
Please tell me what is wrong here? (I also checked other thread but I could not solve my problem)
Question Solved:
I forgot to give the permission to access /usr/local/python25/bin/python. After I did this, the problem solved.
Thank you so much for your help!
1.The exec function just return the last line from the result of the command.
2. The print statement in python (except python 3) automatically adds a newline at the end.
This is the reason you feel nothing was happened.
You can catch the whole output by this way.
exec("/usr/local/python25/bin/python myfile.py 2>&1", $output);
print_r($output);
Kind of an obvious point here, but can you run the python script from a terminal? Does it actually run?
Make sure the script is executable by whatever user PHP is running as - chmod 777 myfile.py, and just to be safe chmod 777 /usr/local/python25/bin/python. Also, make sure the python script is in the same directory as the PHP script, which is what your method of calling it requires.
Try changing your PHP script to this, and tell me what you see: (EDITED)
<?php
// Path to the python script - either FULL path or relative to PHP script
$pythonScript = 'myfile.py';
// Path to python executable - either FULL path or relative to PHP script
$pythonExec = '/usr/local/python25/bin/python';
// Check the file exists and PHP has permission to execute it
clearstatcache();
if (!file_exists($pythonExec)) {
exit("The python executable '$pythonExec' does not exist!");
}
if (!is_executable($pythonExec)) {
exit(("The python executable '$pythonExec' is not executable!"));
}
if (!file_exists($pythonScript)) {
exit("The python script file '$pythonScript' does not exist!");
}
// Execute it, and redirect STDERR to STDOUT so we can see error messages as well
exec("$pythonExec \"$pythonScript\" 2>&1", $output);
// Show the output of the script
print_r($output);
?>
If you want to capture the subprocess' stdout, you should use passthru
Also you don't need the first line of that python script if you're calling the python interpreter directly.
Related
I'm working on classification algorithm in python and it related with php code, to get the right result I should run my python exe then go to the php code.
I want to execute the python by itself with php code. I tried this:
<?php
// outputs the username that owns the running php/httpd process
// (on a system with the "whoami" executable in the path)
echo exec('whoami');
?>
But It's not working, should I know more about path? and how to put my exe in the write path?
<?php
exec("C:\\Users\\posh\\AppData\\Local\\Programs\\Python\\Python35\\python.exe YOUR_PYTHONSCRIPT.py <arguments if any>", $output, $ret_code);
// directly writing python may not work in that case
// give path to python.exe
exec("python YOUR_PYTHONSCRIPT.py <arguments if any>", $output, $ret_code);
// $ret_code : returns the code 0 or 1
// output is an array
?>
I have tried to use exec() with 'whoami' to check if it works and I got the result of
nt authority\system
Now I need to run a .exe file with parameters from php via exec() function.
I tried this in command prompt and it actually runs the program with given parameters. This is the example command.
NOTE the exe file gets 3 inputs (folder, file_name, report_file_nmae)
> ..\..\some_file.exe folder="C:\path_to_folder" param=1.xml report=2.xml
But when I run this command from php file:
exec('..\..\some_file.exe folder="C:\path_to_folder" param=1.xml report=2.xml');
nothing is happening. This is the first time I am using exec() function, so I am not familiar with its details. What is wrong?
I tried using:
\\ instead of \
escapeshellarg() on the directory
added "" around directory folder names
No luck
Addendum:
echo exec($command) // echos < .... why?
or
exec($command, $output);
print_r($output); // Array()
I even changed the permission on the file to full control to all users.
If I call the program from command prompt, I can see the icon appearing next to clock for a second.
But the same call from php will not even call the program.
Edit
Even exec('notepad.exe'); is not working. Something has to be done with php configurations maybe?
I already said that I was new to exec() function. After doing some more digging, I came upon 2>&1 which needs to be added at the end of command in exec().
Thanks #mattosmat for pointing it out in the comments too. I did not try this at once because you said it is a Linux command, I am on Windows.
So, what I have discovered, the command is actually executing in the back-end. That is why I could not see it actually running, which I was expecting to happen.
For all of you, who had similar problem, my advise is to use that command. It will point out all the errors and also tell you info/details about execution.
exec('some_command 2>&1', $output);
print_r($output); // to see the response to your command
Thanks for all the help guys, I appreciate it ;)
You might also try giving the full path to the binary you're trying to run. That solved my problem when trying to use ImageMagick.
I have a python file that I would want to execute whenever a php page is called. The python file is in the same folder as the php file. The python script on execution edits a textfile that the php file accesses.
I have the following code:
<?php
exec("python somefile.py",$output);
$file = fopen("test.txt",'r');
....
For some reason, the python script never gets executed. I know this certainly as I can see it from the changes in the text file.
Since I was not sure if the script was made executable, so I included "python" on the command.
I also ran:
chmod +x somefile.py
just to make sure this was not the reason. But this did not help too.
What should I change to make the php page execute the python script whenever it is called?
This is most likely a permission issue.
Try
echo exec("whoami");
This will let you know who php is running as. Then you need to verify this user can run the python script. If the file was not created by the same daemon that runs python, you will most likely be denied permission.
Update
This will let you know who owns all the files you are working with. The file being written to needs to be writable by the user that is running python. If you are running python from ssh, that is most likely not the same user as when you run python from exec.
echo exec('whoami') . "<br>";
echo exec("ls -l test.txt") . "<br>";
echo exec("ls -l somefile.py") . "<br>";
Update 2
Because I constantly forget this exists.
passthru('python somefile.py 1 2>&1');
This will exec your python file, and output stderr to stdout.
I'm trying to run the following command from a PHP file in a web browser:
exec('festival --tts /var/www/test.txt &');
Unfortunately, nothing happens. I thought of trying 'echo' but there is nothing to return to see if the command is working. I set permissions for test.txt to 777. I also ran the command in a shell and it works just fine - just not when submitted by a PHP script. What am I doing wrong?
Provide the full path to the festival binary, you can find it out on the console with which festival command, then use it in your exec call, like this:
exec('/usr/bin/festival --tts /var/www/test.txt &');
Update:
You need to make sure the folder where you are creating the file has write permission for the user running php which usually is www-data on debian based distros.
If I understand correctly what you're trying to do, you aren't returning the input. The syntax for this is:
exec('command', $output);
where $output is the variable in which the script's output will be stored. Oddly, this output will be returned as an array, so don't forget to implode() it when you're done if you're expecting a string.
Does the festival program print any output? If so, try capturing that. Also check the return value.
exec('/usr/bin/festival --tts /var/www/test.txt &', $output, $return);
Dump the output array, like so:
var_dump($output);
Any non-zero return value usually indicates an error:
echo $return;
I am trying to invoke a script which takes several seconds (web services with 3rd party) using the PHP exec call. After much struggling, I reduced this to the classic hello world example. The calling script looks like:
exec('/usr/bin/php /home/quote2bi/tmp/helloworld.php > /tmp/execoutput.txt 2>&1 &');
When I run this, the output execoutput.txt contains a copy of the invoking script page, not hello world as I expected.
Why can't I get this PHP script to execute using exec? Note that when I change the command to something like ls -l, the output is a directory listing as expected. btw, in case it matters, I did chmod the called script to 755...
Update - I moved the exec call to the end of the calling script and at least now I don't see the calling script executed in the output. Thx to posters and I will try some of these ideas.
Help!
Thanks
Steve
I had this issue also and it turns out this is a bug in php (#11430). The fix is to use php-cli when calling another php script within a php script. So you can still use exec but rather than use php use php-cli when calling it in the browser:
exec("php-cli somescript.php");
This worked for me.
What exec is doing is taking the rightmost command and appending it to your destination. If you have the shebang line in your php script, you shouldn't need to include the binary directive of the php interpreter.
if you just want the script's output, try:
exec('/home/quote2bi/tmp/helloworld.php > /tmp/execoutput.txt 2>&1 &')
however if you do not want the errors to be in the file, you should redirect the STDERR prior to outputting to the file. Like so:
exec('/home/quote2bi/tmp/helloworld.php 2> /dev/null > /tmp/execoutput.txt')
the above should only output the "Hello World" to the execoutput.
Edit:
Interesting you are getting this behaviour. You stated the command "ls" worked. Try making an alias for this and forward it to a file like so:
alias pexec='php /home/quote2bi/tmp/helloworld.php'
then
exec('pexec > /tmp/execoutput.txt 2>&1 &')
it seems to be a problem with the way exec handles input as opposed to the shell itself.
-John
The problem is with PHP itself, it treats everything as $argv in the script. It doesn´t redirect the output to a file ou to /dev/null.
I faced the same problem some time ago. What I did is to create a runscript.php in /opt/php-bin and then inside this script run what It should be running. Something like this:
$script = $argv[1]
$params = implode(' ', array_slice($argv, 2));
$cmd = "{$script} {$params} > /dev/null &";
$output = array();
$return = 0;
exec("php {$cmd}", $output, $return);
exit((int)$return);
And then you call it using:
exec('/opt/php-bin/runscript.php /path/to/your/script.php arg1 arg2')
It´s the only way I managed to get this working.
To avoid the stated problems of PHP in this area, why not put this in inside a shell script? PHP can then execute the shell script which has all the redirections handled internally.
If you need to dynamically change things, then why not write the shell script and then execute it (and of course, clean up afterwards)?
if you are just simply running a php script one possible way to execute the entire code is to use the include() that will run the php file and output any results. You cannot direct the output to a text file but it should appear in the browser window if you're Hello World php script looks like
<?php echo "Hello World!"; ?>
then it will spit that out in the browser. So your second code would look like
<?php include("helloWorld.php"); echo " PHP ROCKS";?>
resulting in a page that would look like,
Hello world! PHP ROCKS
This runs as if you run the script from browser.
This came across while working on a project on linux platform.
exec('wget http://<url to the php script>)
Hope this helps!!