Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top