Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exec() error responses

Tags:

php

exec

Lets take this command for example:

$command = "echo '/just/for/the/test /heh/' | awk '//just// {print $1}'";

When directly copying it inside shell, I get the following error:

awk: cmd. line:1: //just// {print $1}
awk: cmd. line:1:         ^ unterminated regexp

But, when I exec() it, I get status code 1 with no output:

exec($command, $output, $status);

var_dump( $command, $output, $status );

// string(69) "echo '/just/for/the/test /heh/' | awk '//just// {print $1}'"
// array(0) { }
// int(1)

How to retrieve the STDERR part of exec?

like image 265
tomsseisums Avatar asked Feb 19 '23 03:02

tomsseisums


1 Answers

You should redirect stderr to stdout somehow like this

$stout = exec($command . " 2>&1", $output, $status);

See also here PHP StdErr after Exec()

like image 123
Benjamin Seiller Avatar answered Feb 23 '23 01:02

Benjamin Seiller