0

With unvaluable help from people on StackOverflow I managed to run a bash script from php. Now everything is running fine but as the script takes some minutes to end I would like to see progress. In the bash script there are some printf commands that output error/success messages.

As a newbie in php I assumed those messages would appear as they are executed in the bash script. But I don't get any output although the script is finished. I can see the files created and for sure the script finished but I can't see any output from php in the browser.

How can I check the progress when the script is still running? Why php does not seem to end?

Following is the php script.

<?php

echo "<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Strict//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd>
      <html xmlns=http://www.w3.org/1999/xhtml>
      <head>
      <meta http-equiv=Content-Type content=text/html; charset=UTF-8 />
      <title>Title</title>
      <link href=../style.css rel=stylesheet type=text/css>
      </head>
      <body topmargin=0 leftmargin=0>";
echo '<font color=blue>';

echo '------------------------------------------------------------- <br>';
echo 'Inicio de la generación manual de mapas de olas de calor<br>';
echo 'Este proceso tarda varios minutos<br>';
echo '-------------------------------------------------------------<br>';
echo date('h:i:s') . "\n";
$output = shell_exec('/home/meteo/www/RAMS/olas/generar-mapas.bash');
echo "<pre>".$output."</pre>";
echo '</font>';
echo '</body>';
echo '</html>';

?>

I've read some posts in the forum about php, Ajax and jQuery for progress bars but I just know some very basic php. Is there a simple solution for a novice in php? Thanks for your help

4
  • 1
    Pro PHP Tip: don't feel the need to echo absolutely everything. You can simply close the ?>, output HTML, then reopen the <?php when you need to. Pro HTML Tip: the <font> tag is very old-fashioned and should no longer be used. If you want the font to be blue, use CSS. Commented Feb 18, 2015 at 11:59
  • @tom-fenech Agree with the use of CSS, just a copy-paste mistake. Don't need to echo everything but the point is that any single echo appears. Sorry if too basic but what does close ?> means? Is it not closed? the html output does not appear. Thanks for answering. Commented Feb 18, 2015 at 12:12
  • I thought of a solution by sending the output of the bash script to an autorefreshing html open in an iframe. But it seems a tricky workaround, there should be a nice clean php way to do it. Commented Feb 18, 2015 at 12:15
  • 1
    This is general advice, separate to your issue, which is why I only posted it as a comment. You can open and close PHP tags in the middle of your script, so you don't need to write all of your HTML in a PHP string. Hope that makes sense. Commented Feb 18, 2015 at 12:16

1 Answer 1

1

shell_exec() blocks till the child process finishes, use popen() instead:

$p = popen('/home/meteo/www/RAMS/olas/generar-mapas.bash', 'r');
while(!feof($p)) {
    echo fgets($p);
    ob_flush();
    flush();
}
pclose($p);
Sign up to request clarification or add additional context in comments.

3 Comments

Hi @marek and thanks. It seems popen does not work for me. It does not start any process, bash script does not run.
I forgot to add mode, I hope that helps. It might be neccessary to output more bytes, in case there's implicit buffer somewhere.
Just needed to add 2>&1','r' to popen on your answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.