Redirect in PHP exec call creates empty file

It is quite simple and I have no ideas. I am sure there is a quick fix.

exec('echo 123 &> /var/log/123.log'); 

I am sure that this is not about permissions, because the 123.log file is created, but it is just empty. I also tried shell_exec, but it does not create the file at all. Also checked all redirection options, i.e. 1> 2> > .

Using PHP to capture output is not an option, since output in production is huge and I don't want to run into memory problems.

Any ideas appreciated.

By the way, I am using Ubuntu 12.04 LAMP.

+6
source share
3 answers

Try shell_exec without &:

 echo shell_exec("echo 123 > /var/log/123.log"); 
0
source

The only thing that helped was to create a shell script with administrator rights, for example. test.sh:

 #!/bin/bash echo 123 &>> /var/log/123.log 

and execute it as follows:

 shell_exec('[full path to]/test.sh'); 

So, the redirection operator is not important, but everything else (#! Directive, shell_exec).

0
source

Debian and Debian-based Linux distributions such as Ubuntu now use dashes rather than bash like / bin / sh.

&> - extension of bash, dash is not known.

The correct posix-compatible way to write cmd &> file is cmd > file 2>&1

cmd > file 2>&1 works in all posix compatible shells: dash, bash, ksh, zsh, ash ...

So you need to change your code to:

 exec('echo 123 > /var/log/123.log 2>&1'); 
0
source

All Articles