0

I am running a command using shell_exec()

Let's say I have 400 directories and I can not wait for the command to run completely.

Is there a way, I can get the output Asynchronous?

$output = shell_exec('ls');
echo "<pre>$output</pre>";
Sheraz Ahmed
  • 383
  • 4
  • 17

2 Answers2

1
$cmd = $command;

        $descriptorspec = array(
            0 => array("pipe", "r"),   // stdin is a pipe that the child will read from
            1 => array("pipe", "w"),   // stdout is a pipe that the child will write to
            2 => array("pipe", "w")    // stderr is a pipe that the child will write to
        );
        flush();
        $process = proc_open($cmd, $descriptorspec, $pipes, realpath('./'), array());
        echo "<pre>";
        if (is_resource($process)) {
            while ($s = fgets($pipes[1])) {
                print $s;
                flush();
            }
        }
        echo "</pre>";

The Code above Worked perfectly for me, This is copied from another answer I can no longer find. If you put a ping 127.0.0.1 in the $command it works exactly like it does in a terminal.

Sheraz Ahmed
  • 383
  • 4
  • 17
0

Check this: Is there a way to use shell_exec without waiting for the command to complete?

And instead of redirecting to /dev/null you could redirect to a tmp file that you read later.

PaulV
  • 119
  • 3