Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

570
Vistas
Why can't I write to /dev/stdout, but php://stdout works?

I'm trying to write to stdout (or stderr) using PHP with Apache (running in the foreground in Docker).

These works:

file_put_contents( "php://stderr","works" );
file_put_contents( "php://stdout","works" );

$fh = fopen('php://stdout','w'); fwrite($fh, "works");

But these do not:

$stdout = fopen ("/dev/stdout","w"); fwrite($stdout, "fails");
$stderr = fopen ("/dev/stderr","w"); fwrite($stderr, "fails");

echo "fd1 exists:" . (file_exists('/proc/self/fd/1') ? 'yes' : 'no');
echo "fd1 writable:" . (is_writable('/proc/self/fd/1') ? 'yes' : 'no');
echo "stdout exists:" . (file_exists('/dev/stdout') ? 'yes' : 'no');
echo "stdout writable:" . (is_writable('/dev/stdout') ? 'yes' : 'no');

file_put_contents( "/proc/self/fd/1", "fails" );
file_put_contents( "/proc/self/fd/2", "fails" );
file_put_contents( "/dev/stdout", "fails" );

fwrite(STDOUT, 'fails');
fwrite(STDERR, 'fails');

/dev/stdout do exist, but is not writable. also fopen('/dev/stdout') returns FALSE.

fd1 exists:yes
fd1 writable:no
stdout exists:yes
stdout writable:no

More info:

  • I tried setting error_log = /dev/stdout in php.ini, but it doesn't work
  • I tried Docker command RUN ln -sf /dev/stdout stdout.log and then writing to stdout.log in PHP, but nope.

Again, what is php://stdout doing that I don't?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The reason is that Apache forks and changes user to a less privileged one (the "apache" user) which does not have permission to write /dev/stdout. There is no easy way around this, so I ended up with a script.

The script creates a file (pipe) which is connected through a 'cat' process which writes to standard error (as root):

mkfifo -m 600 logpipe
chown apache:apache logpipe
cat <> logpipe 1>&2 &
CAT=$!

# Start apache
/usr/sbin/httpd -D FOREGROUND

# Kill the cat after apache dies
kill $CAT

Now you can set error_log = logpipe in php.ini and all php errors will be sent to stderr.

As for why php://stdout works, my guess is that PHP uses a file handle (to /dev/stdout) which is already opened by Apache.

More details here.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda