如何将被调用程序的输出重定向到文件


How to redirect the output of an invoked program to a file?

我有一些PHP代码,除其他外,使用system()调用ffmpeg来编码视频。当我从命令行运行此命令时,我可以用通常的方式(php mycode.php > mycode.log)将PHP脚本的输出重定向到一个文件,这样不仅可以得到我在PHP中调用的任何打印语句,还可以得到我想要的由ffmpeg生成的内容。终成眷属。

现在,我想通过一个网页在后台启动PHP脚本:我在我的网站上做了一些事情,使它运行:

$the_command = "/bin/php $php_file_name > $log_file_name &";
$the_result = system($the_command, $retval);

这成功地在后台PHP进程中运行我的脚本,我的视频仍然被ffmpeg编码。然而,我在重定向日志文件中得到的唯一东西是脚本中的打印语句——ffmpeg调用的输出被发送到某处的以太。有什么办法我可以得到这些保存在日志文件,以及?

试试这个:

$the_command = "/bin/php $php_file_name > $log_file_name 2>&1";

IMHO -因为ffmpeg将其消息写入stderr。所以,试试这样写:

$res = shell_exec("ffmpeg your_args >/tmp/log 2>&1 &");

这会将stdoutstderr重定向到文件。