|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-11-04 09:49 UTC] rachmel at avaya dot com
Description:
------------
PHP's exec function returns the wrong return value.
When running script from CLI, works perfectly. When running the same script from the web-server context - 90% of the times it returns the wrong value - (-1).
I am using the appWeb embedded server, which is a slim apache-like webserver.
I validated there are no permission running issues, and I couldn't see any log messages sent by PHP that might indicate what the problem is.
Reproduce code:
---------------
<?php
print "<pre>";
for ($i = 0; $i < 10; ++$i) {
exec("/bin/true", $output, $status);
print "exec('/bin/true') returns with <b>$status</b> (expected
0)\n";
}
for ($i = 0; $i < 10; ++$i) {
exec("/bin/false", $output, $status);
print "exec('/bin/false') returns with <b>$status</b> (expected
1)\n";
}
print "</pre>";
?>
Expected result:
----------------
All executions of "true" should print "0" to the screen
All executions of "false" should print "1" to the screen.
Actual result:
--------------
Most of the times, all executions result in an error code of "-1" being printed to the screen.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 14:00:01 2025 UTC |
Another important comment - The funny thing is that the command finishes succesfuly! I tried using a small c program that I wrote: #include <stdio.h> int main(int argc, char **argv) { printf("true was called!"); return 0; } So when printing the $output var, it holds the correct string. It is just the $status var that holds the wrong value.First of all: You're obviously running PHP as CGI since that's the only one you're compiling with that configure line. Also, try this script instead: <?php for ($i = 0; $i < 10; ++$i) { exec("/bin/true", $out, $status); var_dump($out, $status); exec("/bin/false", $out, $status); var_dump($out, $status); } ?>1. Here's the output of your script: array(0) { } int(-1) array(0) { } int(-1) array(0) { } int(-1) array(0) { } int(-1) array(0) { } int(-1) array(0) { } int(-1) array(0) { } int(-1) array(0) { } int(-1) array(0) { } int(-1) array(0) { } int(-1) array(0) { } int(-1) array(0) { } int(-1) array(0) { } int(-1) array(0) { } int(-1) array(0) { } int(-1) array(0) { } int(1) array(0) { } int(-1) array(0) { } int(-1) array(0) { } int(-1) array(0) { } int(-1) 2. Regarding the CGI mode. Maybe I don't understand the concept of CGI well enough - I will try to explain my setting: a. I compile php and create the libphp5.so. Notice the "'--enable-embed=shared'" directive in the configure line. b. I compile my web-server (appWeb). It has an internal module for running php scripts which dynamically links to the libphp5.so. Is this referred to as CGI mode or SAPI mode?