Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am using exec function to execute the specific executable files in php .

exec ( $file , $output , $return_value  ) ;

When the given file executed successfully I am able to get the output in the second argument by checking the return values , So, It is working fine. But My requirement is when the command execution getting failed due to some reason. I need to get that error message of that executed program . What I need to do to get the error . through second argument we can get the successful output only . Not error message.

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
209 views
Welcome To Ask or Share your Answers For Others

1 Answer

The second argument $output only captures STDOUT from your executable. Error messages are usually sent to STDERR so that they easily can be written to an error log or similar, but this means that you won't see them when you call exec.

If this is a linux system, you could append 2>&1 to your command, in order to redirect STDERR to STDOUT. I haven't tried this, but it should forward the error messages to your $output variable.

Edit:

I've read up on it on www.php.net/exec, and it seems this would work.

exec($file.' 2>&1', $outputAndErrors, $return_value);

It is also possible to redirect the errors to a temporary file and read them separately.

exec($file.' 2> '.$tmpFile, $outputOnly, $return_value);

Edit 2

It seems windows also uses this Bourne style output redirecting syntax, so the examples should work for windows too.

More on input and output streams


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...