tomsseisums
tomsseisums

Reputation: 13367

exec() error responses

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?

Upvotes: 4

Views: 815

Answers (2)

Benjamin Seiller
Benjamin Seiller

Reputation: 2905

You should redirect stderr to stdout somehow like this

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

See also here PHP StdErr after Exec()

Upvotes: 5

Vlad Balmos
Vlad Balmos

Reputation: 3412

with exec() the only way i can think of is to redirect STDERR to STDOUT in the command you are executing, like:

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

an alternative to exec() is to use the proc_open() family of function. With proc_open() you execute the command and open file pointers to STDIN, STDOUT and STDERR from which you can read / write

see the manual for more info

Upvotes: 3

Related Questions