0

I'm running a simple command in a loop the command itself is ffmpeg, but I do not believe it's related to the issue so, I have:

exec($exec.' 2>&1', $output, $return);
if($return)
{
    foreach($output as $line)
    {
      file_put_contents($log_file, $line, FILE_APPEND);
    }
}

This way, if anything goes wrong with the command I can read the output in the log. It works, however $output contains the entire shell history of the command. To clarify: every time an error occurs, all output that was generated by the particular command (including hundreds of successful executions from throughout the day) is dumped to the file. What should be a 5 line error being written is instead the entire 1000+ line history. I used the exact same code on CentOS and it gave me the expected output of only the output generated by the instance most recently executed.

Jesse Adam
  • 415
  • 3
  • 14

1 Answers1

0

From the documentation:

Note that if the array already contains some elements, exec() will append to the end of the array. If you do not want the function to append elements, call unset() on the array before passing it to exec().

I can't explain why it worked differently on CentOS.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • unset($output); worked perfect. Thanks. I guess CentOS was clearing $output every time exec() is called – Jesse Adam Aug 15 '13 at 18:41
  • I don't see how the OS can do that, this is part of PHP. Maybe it's an old version of PHP, although there are no version comments in the doc that say it has changed. – Barmar Aug 15 '13 at 19:21