PHP exec调用中的重定向创建空文件

tis*_*hma 6 php shell io-redirection

这很简单,而且我没有想法.我确信有一个快速的解决方法.

exec('echo 123 &> /var/log/123.log');
Run Code Online (Sandbox Code Playgroud)

我确定它不是关于权限,因为创建了文件123.log,但它只是空的.我也试过shell_exec,但它根本不创建文件.还尝试了重定向的所有变体,即1> 2> >.

使用PHP捕获输出不是选项,因为生产中的输出很大,我不想遇到内存问题.

任何想法都赞赏.

顺便说一下,我正在使用Ubuntu 12.04 LAMP.

Sam*_*ner 2

Debian 和基于 Debian 的 Linux 发行版(例如 Ubuntu)现在使用 dash 而不是 bash 作为 /bin/sh。

&>是一个 bash 扩展,破折号不知道。

正确的 posix 兼容的编写方式cmd &> file是cmd > file 2>&1

cmd > file 2>&1适用于所有 posix 兼容的 shell:dash、bash、ksh、zsh、ash ...

所以你需要将代码更改为:

exec('echo 123 > /var/log/123.log 2>&1');
Run Code Online (Sandbox Code Playgroud)