|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #22999 proc_close() sometimes returns -1 when called process exited with status of 0
Submitted: 2003-04-01 04:17 UTC Modified: 2005-08-06 01:00 UTC
Avg. Score:4.5 ± 1.1
Reproduced:13 of 15 (86.7%)
Same Version:2 (15.4%)
Same OS:2 (15.4%)
From: phil at concretecomputing dot co dot uk Assigned: wez (profile)
Status: No Feedback Package: Filesystem function related
PHP Version: 5CVS OS: sun os
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2003-04-01 04:17 UTC] phil at concretecomputing dot co dot uk
I'm trying to run a command via proc_open(). The command always succeeds (exits with status of 0), so I would expect that proc_close() would always return 0. Most of the time it does, but sometimes it returns -1.

Here is a script which reproduces the problem. On my system, if you run the script many times, most times the status reported will be 0, but occasionally it will be -1.


$inputArray = array(
  0 => array("pipe", "r"),                            // stdin is a pipe that the child will read from
  1 => array("pipe", "w"),                            // stdout is a pipe that the child will write to
  2 => array("pipe", "w")                             // stderr is a pipe that the child will write to

$process = proc_open("ps -ef", $inputArray, $outputArray);                

if (is_resource($process)) {


  // Grab the output
  while(!feof($outputArray[1])) {
    $output .= fgets($outputArray[1], 1024);       

  // Extract any error output
  $errorOutput = "";
  while(!feof($outputArray[2])) {
    $errorOutput .= fgets($outputArray[2], 1024);

  // It is important that you close any pipes before calling proc_close() in order to avoid a deadlock

  $status = proc_close($process);    

  echo "proc_close() return result: $status<br>\n";            
  echo "ps error output: [$errorOutput]<br>\n";
  echo "ps output: [$output]<br>\n";                 

echo "Done<br>\n";


PHP is compiled as follows:

'./configure' '--with-apxs=/usr/local/apache1.3.27-nerens3.4/bin/apxs' '--without-mysql' '--enable-track-vars' '--enable-sigchild' '--with-oci8=/opt/oracle/product/9.0.1' '--enable-apc' '--with-xml' '--with-expat-dir=/usr/local/expat' '--with-zlib=/usr/local/zlib' '--with-curl=/usr/local/curl' '--with-mhash=/usr/local/mhash' '--with-mcrypt=/usr/local/libmcrypt' 

I have tried to have a look at what is going on using truss and I think it may be caused by another function within php doing a wait() and getting the exit status for the child so that the wait() in proc_close returns with ECHLD.

While testing using the above script, I have found that if I add a call to sleep after the proc_open() call then proc_close() _always_ returns -1 (and the sleep() call has no effect)


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2003-04-01 05:00 UTC]
This is a known issue when compiling with

I will see if it can be fixed.
 [2003-04-17 13:37 UTC] michael at six dot de
We have the same issue with pclose()


$fp = popen("ls","r");
if ($fp) {
	while(! feof($fp)) {
	$status = pclose($fp);
	print "pclose: status=$status\n";

Most of the time $status is -1, sometimes 0.
PHP-4.3.2RC1, Solaris 7, --enable-sigchild

PHP is compiled with --enable-sigchild because the oracle client is needed too, this was recommended in former bug descriptions. Is this still the case for oracle?
 [2003-10-08 07:23 UTC]
Won't fix in PHP 4 (changes required are too messy to
introduce to stable branch), will fix in PHP 5.
 [2005-07-30 00:55 UTC]
Please try using this CVS snapshot:
For Windows:

 [2005-08-06 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2006-08-24 21:04 UTC] lindsay at bitleap dot com
I still see this problem running php 5.1.4 on linux kernel 2.6.16.  I do use pcntl and the configure command is pasted below.  If there is any additional info I can provide to help, let me know.


Configure Command =>  './configure' '--prefix=/usr/lib64/php5' '--sysconfdir=/etc' '--cache-file=./config.cache' '--with-libdir=lib64' '--enable-cli' '--disable-cgi' '--with-config-file-path=/etc/php/cli-php5' '--with-config-file-scan-dir=/etc/php/cli-php5/ext-active' '--without-pear' '--disable-bcmath' '--with-bz2' '--disable-calendar' '--disable-ctype' '--without-curl' '--without-curlwrappers' '--disable-dbase' '--disable-dom' '--disable-exif' '--without-fbsql' '--without-fdftk' '--disable-filepro' '--disable-ftp' '--with-gettext' '--with-gmp' '--disable-hash' '--without-hwapi' '--without-iconv' '--without-informix' '--without-kerberos' '--disable-libxml' '--disable-mbstring' '--with-mcrypt' '--enable-memory-limit' '--without-mhash' '--without-ming' '--without-msql' '--without-mssql' '--with-ncurses' '--with-openssl' '--with-openssl-dir=/usr' '--enable-pcntl' '--disable-pdo' '--with-pgsql' '--with-pspell' '--without-recode' '--disable-simplexml' '--disable-shmop' '--with-snmp' '--disable-soap' '--enable-sockets' '--without-sybase' '--without-sybase-ct' '--enable-sysvmsg' '--enable-sysvsem' '--enable-sysvshm' '--without-tidy' '--disable-tokenizer' '--disable-wddx' '--disable-xml' '--disable-xmlreader' '--disable-xmlwriter' '--without-xmlrpc' '--without-xsl' '--with-zlib' '--disable-debug' '--enable-dba' '--without-cdb' '--with-db4' '--without-flatfile' '--without-gdbm' '--without-inifile' '--without-qdbm' '--without-freetype-dir' '--without-t1lib' '--disable-gd-jis-conv' '--disable-gd-native-ttf' '--with-jpeg-dir=/usr' '--with-png-dir=/usr' '--with-xpm-dir=/usr/X11R6' '--with-gd' '--without-mysqli' '--with-readline' '--without-libedit' '--without-mm' '--without-sqlite'
 [2008-04-21 19:29 UTC] liamr at umich dot edu suggests that --enable-sigchild prevents pclose and proc_close from obtaining the proper exit status.

This bug (22999) says that this wouldn't be fixed in PHP4, but would be in PHP5.  This is still a problem with 5.2.x.

The tip suggested in

    $ret = (proc_close($proc) >> 8) & 0xff;

just changes the "-1" into "255"

Do you have any suggestions for anyone who needs to obtain the actual exit code using pclose() or proc_close() /and/ has to use --enable-sigchild (because of oracle)?
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Fri May 07 05:01:42 2021 UTC