What can I send back to the browser while I wait for PHP execution?

Posted by Matt Malesky on Server Fault See other posts from Server Fault or by Matt Malesky
Published on 2012-06-26T21:25:49Z Indexed on 2012/06/27 9:17 UTC
Read the original article Hit count: 359

Filed under:

So....I have a PHP page that involves a lot of backend execution, namely 'exec' calls to run shell commands on the host server.

This can take upwards of a few minutes depending on the calls involved. (If you look below, each recursion through the exec calls is mounting a LUN; I'd like to sometimes do upwards of 100 per execution.) I'm curious on what I can do to send content back to the browser (and prevent it from timing out).

<!DOCTYPE html>
<html>
  <head>
    <title>sfvmtk</title>
  </head>
  <body>
    <?php
      // TEMPORARY VARIABLES FOR TESTING
      $hba = 'vmhba38';
      $svip = '10.10.20.100';
      $targets = array ( 0 => array ( 'iqn' => 'iqn.2010-01.com.sf:t5np.esxtest.41',
                                      'account' => 'esx',
                                      'isecret' => 'isecret00000',
                                      'tsecret' => 'tsecret00000'
                                    ),
                         1 => array ( 'iqn' => 'iqn.2010-01.com.sf:t5np.esxtest2.42',
                                      'account' => 'esx2',
                                      'isecret' => 'isecret00001',
                                      'tsecret' => 'tsecret00001'
                                    )
                       );

      $hostname = $_REQUEST['hostname'];
      $username = $_REQUEST['username'];
      $password = $_REQUEST['password'];

      foreach ($targets as $ctarget) {
        exec('esxcli -s '.$hostname.' -u '.$username.' -p '.$password.' iscsi adapter discovery statictarget add -A '.$hba.' -a '.$svip.' -n '.$ctarget['iqn'], $out);
        exec('esxcli -s '.$hostname.' -u '.$username.' -p '.$password.' iscsi adapter target portal auth chap set -A '.$hba.' -a '.$svip.' -N '.$ctarget['account'].' -d uni -l required -n '.$ctarget['iqn'].' -S '.$ctarget['isecret'], $out);
        exec('esxcli -s '.$hostname.' -u '.$username.' -p '.$password.' iscsi adapter target portal auth chap set -A '.$hba.' -a '.$svip.' -N '.$ctarget['account'].' -d mutual -l required -n '.$ctarget['iqn'].' -S '.$ctarget['tsecret'], $out);
      }
      exec('vicfg-rescan --server '.$hostname.' --username '.$username.' --password '.$password.' '.$hba, $out);
    ?>
  </body>
</html>

© Server Fault or respective owner

Related posts about php