7

It's quite simple, and I'm out of ideas. I'm sure there is a quick workaround.

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

I'm sure it's not about the permissions, because the file 123.log is created, but it's just- empty. I've also tried shell_exec, but it doesn't create the file at all. Also tried all variants of redirection, i.e. 1> 2> >.

Using PHP to capture the output is not the option, as the output in production is huge, and I don't want to run into memory issues.

Any ideas appreciated.

Btw, I'm using Ubuntu 12.04 LAMP.

7
  • have you tried echo 123 &> /var/log/123.log in your terminal? Commented Apr 18, 2013 at 8:31
  • are you 100% positive its not permissions? Try doing the echo statement manually in terminal and see if it works Commented Apr 18, 2013 at 8:31
  • Verified, this did not work for me either. Using PHP 5.3.10-1ubuntu3.4 with Suhosin-Patch. Commented Apr 18, 2013 at 8:37
  • the file is created. how could it be permissions? it must be something about how redirection is done. Commented Apr 18, 2013 at 8:41
  • I have some progress with echo 123 2>&1 1>> /var/log/123.log, but now only stdout is captured. stderr is not. Commented Apr 18, 2013 at 8:45

3 Answers 3

2

Debian and Debian based Linux distributions like Ubuntu are using dash and not bash as /bin/sh by now.

&> is a bash extension, the dash does not know about.

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');
Sign up to request clarification or add additional context in comments.

4 Comments

It's just about of order. just exec('echo 123 > ./123.log &'); also fine
@AlexandrKapustin: No, that would just run echo 123 > ./123.log in the background.
I'm back to this one more than a year later :) Yes, I confirm this works on ubuntu 16.04 with default php (7.0.30).
@ling: The example will write all lines from stdout and stderr to the log file, and the exec() function will return an empty string. Maybe your case is somewhat different. I suggest you ask a new question about the exact command you are executing.
0

Try shell_exec without &:

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

Comments

0

Only thing that did help was to create a shell script with exec permissions, e.g. test.sh:

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

and execute it like this:

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

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

3 Comments

The reason is that cli redirections are handled by the shell, and only this way we are sure that bash is executed. I'm not sure how exec interprets commands otherwise.
Just for completeness: another way to make sure the bash is used is shell_exec('/bin/bash -c \'echo 123 &> /var/log/123.log\''). But I admit that is very ugly.
Thanks @SamuelKirschner The reason I did not accept my own answer was that it's arguably at least as ugly as the one in your comment.... Happy to accept the posix-compliant one. I haven't had time to verify it until today :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.