root root - 6 months ago 37
PHP Question

shell_exec echo'ing too fast or too late

So I read throug this article:
and found this:

echo "Starting ffmpeg...\n\n";
echo shell_exec("ffmpeg -i input.avi output.avi >/dev/null 2>/dev/null &");
echo "Done.\n";

the code works almost perfectly fine, but the only thing that bothers me is, that because the shell_exec is being executed in the background (so there is no loading sign on the tab the whole time) all the echo's are being executed immediatly. This means, that the "Done" echo is being written before ffmpeg finished its task.

But when I remove the '&' the site does also what it is intended to do but now it waits until the shell_exec is finished until it echo's out the "Starting ffmpeg..."

So I kinda have the problem of figuring out how to get to such an order:

1) echo something
2) execute commad and wait until its finished.
3) echo again

I am a total beginner to php but have experience in programming overall. Please have mercy.

Thank you for your time!

raj raj

You might want to use flush before you do your shell_exec. There are a few caveats to using this which are well explained in the php documentation page. Hope this helps.

  echo "Starting ffmpeg...\n\n";

  echo shell_exec("ffmpeg -i input.avi output.avi >/dev/null 2>/dev/null");

  // a sleep could effectively be the same as your shell_exec operation
  // sleep(5);

  echo "Done.\n";