note 48965 deleted from function.exec by cmb
| From: | cmb@php.net | Date: | Sun, 24 Nov 2019 08:53:00 +0000 |
| Subject: | note 48965 deleted from function.exec by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-213210@lists.php.net to get a copy of this message | ||
Note Submitter:
----
I wanted my script to:
* execute an external command.
* check if the execution was ok. (i.e. the return level)
* log the error output if the execution wasn't ok.
* not print the command's output in my script's output.
I saw the exec, system, shell_exec and passthru functions, and deduced that the solution was to
redirect the standard error (stderr) to the standard output (stdout). It's not very clean,
since it mixes stderr with stdout, and I only wanted to log the stderr. But it seems to be the only
solution (suggestions are welcome).
This is the code I use:
<?php
$com= "ls"; # command
exec("$com 2>&1", $out, $err);
if ($err) my_log_func(join("\n", $out));
?>