note 48965 modified in function.exec by didou
| From: | didou@php.net | Date: | Thu, 13 Jan 2005 16:02:24 +0000 |
| Subject: | note 48965 modified in function.exec by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-83197@lists.php.net to get a copy of this message | ||
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));
?>
--was--
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 stderr, 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));
?>
http://php.net/manual/en/function.exec.php