note 48965 added to function.exec
| From: | php-general at lists dot php dot net | Date: | Thu, 13 Jan 2005 12:38:33 +0000 |
| Subject: | note 48965 added to function.exec | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-83189@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 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));
?>
----
Manual Page -- http://www.php.net/manual/en/function.exec.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+48965
Delete -- http://master.php.net/manage/user-notes.php?action=delete+48965&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+48965&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+48965&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+48965&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+48965&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+48965&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+48965&report=yes
Search -- http://master.php.net/manage/user-notes.php