I have created a small php script locally that runs a java application in command line. This java application continuously runs and never finishes. As it runs, it outputs command line text. Here is the code:
<?php
set_time_limit(0);
$command = "java -Xms124M -Xmx124M -jar myapp.jar";
$end = " 2>&1";
$in = $command . $end;
$out = exec($in);
var_dump($out);
?>
My problem is that the output is never printed because the app never stops running. Is there a way to get the php to print out each line that is returned as the app is running?
Hopefully I am making sense here (Let me know if I am not).
You might want to take a look at the passthru function and the popen function. These should return output as it occurs (although passthru might buffer the output).
One solution would be to launch your process as a background process and redirect output to a file. You could then read the output file in PHP, but you really shouldn't leave PHP running like that, especially since your java process is expected to never end. A better solution would be to use AJAX polling to have PHP return any recent updates to the output file every few seconds or something.
Related
I have called a python script within my PHP page using the code below:
echo shell_exec("python object_detection_tutorial_cmsc265.py");
The python script generates image files in a loop until a certain condition is met. Example is the code below:
while (True):
cv2.imwrite('image' + str(loop) + '.png',image);
if (not condition):
break
When the HTML/PHP page is exited in the browser, I can see that the images are still increasing in number, that means the python is still executing. How will I stop the python script upon exiting PHP page?
Assuming your Python script is running in background, like this, you cannot.
If you want to handle this you will have to handle pid.
What following is kind of dirty, well using any kind of shell_exec is dirty anyway, but keep this in mind.
What you could do:
Make the Python script echo its pid at script startup
Use exec PHP function over shell_exec to get only script's first output line
Kill this pid before leaving your PHP script
Python
import os
print(os.getpid())
# your script
PHP
// code
$pid = intval(exec('python foo.py'))
exec("kill $pid")
Well, I'm not PHP developer, but google just told me it may be better to use posix_kill over exec("kill $pid").
Again, this is not clean code, it's hacky and depends a lot of the system.
I'm trying to compile an executable via PHP with msbuild which compiles my C# source, the majority of the script relies on the executable being created so it must wait for msbuild to compile the source.
If I don't put any sort of while loop it will compile fine and the executable is created but the problem is the rest of the script executes to fast and the end result isn't correct.
so at the moment I'm using this..
exec('C:\Windows\Microsoft.NET\Framework\v3.5\MSBuild.exe C:\Users\Administrator\AppData\Roaming\Compile\Myprogram\Myprogram.sln /p:Configuration=Release');
while (!file_exists('C:\Users\Administrator\AppData\Roaming\Compile\Myprogram\bin\Release\Myprogram.exe')) sleep(1);
However in this scenario it's almost as if the exec command never gets ran at all. It gets stuck in an infinite loop and eventually times out resulting in the exe never being compiled.
Any suggestions on the proper way to go about this?
Try running it as follows:
$output = array();
$cmd = 'C:\Windows\Microsoft.NET\Framework\v3.5\MSBuild.exe C:\Users\Administrator\AppData\Roaming\Compile\Myprogram\Myprogram.sln /p:Configuration=Release && exit';
exec($cmd, $output);
Am running a ruby file using php. When I run the php in the terminal it runs but the execution is a bit slow. When I run the php file on my browser results don't display. When I use a simple command for example 'ls' it runs fine in the terminal and web browser.
below is the script am using in my php file.
echo "<pre>";
$display = system('ruby /home/user/ruby-grok/examples/test.rb');
echo "</pre>";
Without seeing the contents of test.rb we can't tell you why it isn't outputting something. The file could be empty for all we know.
At the same time, you're capturing any output of the system command and storing it in $display but you're not printing that value. Is the Ruby script returning something or supposed to print it?
Again, without knowing what's in that script we can't help in any real way.
Basically my situation is as follows.
Upload file
Run external process on file (which generates another file)
When external process is down, process the generated file.
Currently in PHP I run the program as follows:
$cmd = 'cd the_directory/; ./the_program'
system($cmd);
The program runs fine and everything, but the program at the end says "Press Enter to exit..." And thus Apache is hanging indefinitely as this program is waiting for user input. Our partner declares that they have this program integrated into their backend flawlessly and does not experience this issue. Up until now all external programs that i have executed in PHP exit without requiring user input which seems to be the norm for this situation.
It seems to me that the code should just simply not have the end message requiring user input. Am I missing something? Or is there a way to get around this? Or do they just need to change their code?
Thanks!
I think you should try proc_open.
With it you can not only execute an external command as a process, but also set pipes to get and send information to that process.
Take a close look to the third parameter of this function, and study the example in the PHP manual for this function, where you can see something like this:
fwrite($pipes[0], '<?php print_r($_ENV); ?>');
so, you can write what you need to the input pipe of the process you've just opened.
if you use Windows environment:
$run_cmd = "cmd /c c:/app_folder/app.exe";
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run($run_cmd, 0, false);
Note that the latter will cause the process window to close on end
If you use UNIX (haven't tested it myself yet):
exec('\app_folder\app &');
I have a PHP website and I would like to execute a very long Python script in background (300 MB memory and 100 seconds). The process communication is done via database: when the Python script finishes its job, it updates a field in database and then the website renders some graphics, based on the results of the Python script.
I can execute "manually" the Python script from bash (any current directory) and it works. I would like to integrate it in PHP and I tried the function shell_exec:
shell_exec("python /full/path/to/my/script") but it's not working (I don't see any output)
Do you have any ideas or suggestions? It worths to mention that the python script is a wrapper over other polyglot tools (Java mixed with C++).
Thanks!
shell_exec returns a string, if you run it alone it won't produce any output, so you can write:
$output = shell_exec(...);
print $output;
First off set_time_limit(0); will make your script run for ever so timeout shouldn't be an issue. Second any *exec call in PHP does NOT use the PATH by default (might depend on configuration), so your script will exit without giving any info on the problem, and it quite often ends up being that it can't find the program, in this case python. So change it to:
shell_exec("/full/path/to/python /full/path/to/my/script");
If your python script is running on it's own without problems, then it's very likely this is the problem. As for the memory, I'm pretty sure PHP won't use the same memory python is using. So if it's using 300MB PHP should stay at default (say 1MB) and just wait for the end of shell_exec.
A proplem could be that your script takes longer than the server waiting time definied for a request (can be set in the php.ini or httpd.conf).
Another issue could be that the servers account does not have the right to execute or access code or files needed for your script to run.
Found this before and helped me solve my background execution problem:
function background_exec($command)
{
if(substr(php_uname(), 0, 7) == 'Windows')
{
pclose(popen('start "background_exec" ' . $command, 'r'));
}
else
{
exec($command . ' > /dev/null &');
}
}
Source:
http://www.warpturn.com/execute-a-background-process-on-windows-and-linux-with-php/
Thanks for your answers, but none of them worked :(. I decided to implement in a dirty way, using busy waiting, instead of triggering an event when a record is inserted.
I wrote a backup process that runs forever and at each iteration checks if there is something new in database. When it finds a record, it executes the script and everything is fine. The idea is that I launch the backup process from the shell.
I found that the issue when I tried this was the simple fact that I did not compile the source on the server I was running it on. By compiling on your local machine and then uploading to your server, it will be corrupted in some way. shell_exec() should work by compiling the source you are trying to run on the same server your are running the script.