Ruby results are not showing in web browser - php

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.

Related

PHP runs Perl script from command line successfully but I get no output displayed

I have a problem displaying the results of a Perl script that I am calling from my PHP webpage. The Perl script constantly monitors a socket and will display the output of this when run from the command line and also saves the output to a file. I know the Perl script is being called and running successfully as the text file is being updated but I do not get the output on the webpage as I was hoping for.
I have tried using the system(), exec(), passthru() and they all allow the Perl script to run but still with no output on the webpage so I am obviously missing something. Am I using the correct functions? Is there a parameter that I need to add to one of the above to push the output back to the webpage that calls the Perl script?
One example of what I have tried from the PHP manual pages:
<?php
exec('perl sql.pl', $output, $retval);
echo "Returned with status $retval and output:\n";
print_r($output);
?>
Edited to include output example as text instead of image as requested.
# perl sql.pl
Connecting to the PBX 192.168.99.200 on port 1752
04/07 10:04:50 4788 4788 3256739 T912 200 2004788 A2003827 A
I'm no PHP expert, but I guess that exec waits for the external program to finish executing before populating the $output and $return variables and returning.
You say that your sql.pl program "constantly monitors a socket". That sounds like it doesn't actually exit until the user closes it (perhaps with a Ctrl-C or a Ctrl-Z). So, presumably, your PHP code sits there waiting for your Perl program to exit - but it never does.
So I think there are a few approaches I'd investigate.
Does sql.pl have a command-line option that tells it to run once and then quit?
Does PHP have a way to send a Ctrl-C or Ctrl-Z to sql.pl a second or so after you've started it?
Does PHP have a way to deal with external programs that never end? Can you open a pipe to the external process and read output from it a line at a time?

How to run a perl script from php with variables

I am trying to run a perl script from php that requires parameters to be passed to the perl script to run correctly. The following is the correct usage of the perl script from the linux terminal:
/home/user/test.pl -a alpha -b beta
or just
/home/user/test.pl -a alpha
I have execute permissions on the script and can run it without any parameters and the correct usage from the script is displayed back to my browser.
Below is the PHP code that works by displaying the usage back to my browser:
$result = shell_exec('/home/user/test.pl');
echo $result;
And the following is the problem code which I can not for the life of me figure out:
$test = $_POST['test'];
$result = shell_exec('/home/user/test.pl -a'.' '.$test);
echo $result;
Can anyone tell me what it is that I am missing to make this work correctly?
Thank you for the help.
My issue resided within the perl script itself and a specific line that was trying to output to a log file which the apache user did not have access to. I was calling the script correctly the whole time but once I was able to get to the server side logs (granted by system admin) I saw the issue was buried within the Perl script and not in php.

Waiting for file to be created

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);

How to execute a batch script from PHP

Server Specs
Windows 2008Rc2
IIS7
mysql
PHP 5.3
I have a batch file that does a mysql dump and then zips up the contents and makes it available for download in a public folder. Now i know this scripts works because it runs fine every-time i run it manually, but i can't seem to get it to work through php.
Basically I would like to just be able to call a php page that will run this batch file.
I know that exec is enabled as im able to use shell_exec to ping google.com, and i can get the output back.
I've tried with just system() and exec(), but still nothing. In some cases it looks like the page is working, but it just sits on the loading prompt.
I've searched high and low trying a million different combinations of commands, but none of them seem to work for me.
I've been reduce to trying this simple command, as i can get ping to work from this. Although when the page is called it only displays the echo statements.
<?php
echo "Starting...";
echo shell_exec("C:\inetpub\wwwroot\DBZIP2");
echo "Success!";
?>
I've also tried this, but the page just hangs on a loading screen and doesn't display the echo commands.
<?php
echo "Starting...<br><br>";
system("cmd /c START C:\inetpub\wwwroot\DBZIP2");
echo "Success!";
?>
Not really sure where to go from here. I've tried pathing the cmd.exe file, but that made no difference. Is there an easier way to do this? Another programming Language perhaps? Any help is appreciated.
Try executing using "shell_exec" with a full path to CMD.exe
<?php
echo "Starting...<br><br>";
shell_exec("c:\WINDOWS\system32\cmd.exe /c START C:\inetpub\wwwroot\DBZIP2");
echo "Success!";
?>

Print out shell_exec as it runs

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.

Categories