Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: proc_close - Manual
[go: Go Back, main page]

PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

proc_get_status" width="11" height="7"/> <passthru
Last updated: Fri, 20 Jun 2008

view this page in

proc_close

(PHP 4 >= 4.3.0, PHP 5)

proc_close — proc_open() で開かれたプロセスを閉じ、 そのプロセスの終了コードを返す

説明

int proc_close ( resource $process )

proc_close()pclose() と似ていますが、proc_open() で開かれたプロセスに対してのみ機能するという点で異なります。 proc_close() は、プロセスが終了するまで待った後で、 終了コードを返します。もし、そのプロセスに対してパイプが開かれていた場合は、 デッドロックを避けるため、fclose() 関数で、 この関数を呼び出す前にそれらを閉じておかなくてはなりません - パイプが開いている間、子プロセスは終了できないからです。

パラメータ

process

閉じられる proc_open() リソース

返り値

実行していたプロセスの終了状態を返します。



proc_get_status" width="11" height="7"/> <passthru
Last updated: Fri, 20 Jun 2008
 
add a note add a note User Contributed Notes
proc_close
proc_close_at_php_net at mailfilter dot com dot ar
10-Jun-2008 09:51
In response to the problem mentioned by ashnazg at php dot net about --enable-sigchild masking the real exit status, this is how I tricked it (Linux):

$descriptorspec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("pipe", "w"));
$cmd = "dosomethingatshell";
// My hack:
$cmd = "( $cmd ) 3>/dev/null ; echo \$? >&3";
$descriptorspec[3] = array("pipe","w");
// .... execute ....
$res = proc_open($cmd, $descriptorspec, $pipes);
if( is_resource($res) )
{
    .....
    // Get the exit status for the original command
    $status = (int) str_replace("\n","",stream_get_contents($pipes[3]));
    foreach($pipes as $pipe) fclose($pipe);
    $n = proc_close($res);
    // $n is not useful; $status has the actual exit status
    // Notice that $status is not the termination status,
    // as proc_close() should return but only the *exit* status
}
morrisdavidd at gmail dot com
05-Jun-2008 08:41
Consider the following pseudo code:

$SOME_PROCESS = proc_open(/* something here */);
...
$status = proc_get_status($SOME_PROCESS);
...
$exitCode = proc_close($SOME_PROCESS);

If the external program has exited on its own before the call to proc_get_status, then $exitCode == -1

So consider using:
$actualExitCode = ($status["running"] ? $exitCode : $status["exitcode"] );
noname at bspamfree dot org
03-Apr-2008 05:44
According to the discussion associated with bug #17538, the "termination status" as returned by this function is not the same thing as the exit code ($? in BASH). To extract the exit code from the termination status, something like this is needed:

$exitcode = (proc_close($proc) >> 8) & 0xff;
ashnazg at php dot net
06-Oct-2007 06:25
It seems that if you configured --enable-sigchild when you compiled PHP (which from my reading is required for you to use Oracle stuff), then return codes from proc_close() cannot be trusted.

Using proc_open's Example 1998's code on versions I have of PHP4 (4.4.7) and PHP5 (5.2.4), the return code is always "-1".  This is also the only return code I can cause by running other shell commands whether they succeed or fail.

I don't see this caveat mentioned anywhere except on this old bug report -- http://bugs.php.net/bug.php?id=29123
e-t172 at e-t172 dot net
19-Oct-2005 02:59
Just an improvement of my precedent function :

<?php
function proc_close_nobug($proc)
{
   
$status = proc_get_status($proc);
   
exec('kill '.$status['pid'].' 2>/dev/null >&- >/dev/null');
   
proc_close($proc);
}
?>

In fact, proc_close() works when called after "kill". This is useful because it doesn't generate "defunct processes" as the precedent version.
oohay251 at yahoo dot com
15-Sep-2005 11:06
From various Internet posts and recent experience, I have observed that you cannot rely on proc_close returning the accurate return code of the child process. The return code also depends on wether or not you read from the stdout/stderr pipes, as my example shows. I work around this by writing the exit code to an additional file descriptor.

<?
$descriptorspec
= 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
   
);
   
$proc = @proc_open("/bin/ls -l /etc/passwd", $descriptorspec, $pipes);
   
fclose($pipes[0]);
   
$output = array();
    while (!
feof($pipes[1])) array_push($output, rtrim(fgets($pipes[1],1024),"\n"));
   
fclose($pipes[1]);
    while (!
feof($pipes[2])) array_push($output, rtrim(fgets($pipes[2],1024),"\n"));
   
fclose($pipes[2]);
   
$exit=proc_close($proc);
   
print_r($output);
    echo
"exitcode $exit\n\n";

$descriptorspec = 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
   
);
   
$proc = @proc_open("/bin/ls -l /etc/passwd", $descriptorspec, $pipes);
   
fclose($pipes[0]);
   
fclose($pipes[1]);
   
fclose($pipes[2]);
   
$exit=proc_close($proc);
    echo
"exitcode $exit\n\n";

$descriptorspec = 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
      
3 => array('pipe', 'w'), // stderr is a pipe that the child will write to
   
);
   
$proc = @proc_open("/bin/ls -l /etc/passwd;echo $? >&3", $descriptorspec, $pipes);
   
fclose($pipes[0]);
   
$output = array();
   
//comment next line to get correct exicode
   
while (!feof($pipes[1])) array_push($output, rtrim(fgets($pipes[1],1024),"\n"));
   
fclose($pipes[1]);
    while (!
feof($pipes[2])) array_push($output, rtrim(fgets($pipes[2],1024),"\n"));
   
fclose($pipes[2]);
    if (!
feof($pipes[3])) $output['exitcode']=rtrim(fgets($pipes[3],5),"\n");
   
fclose($pipes[3]);
   
proc_close($proc);
   
print_r($output);
?>

Outputs on my system:

Array
(
    [0] => -rw-r--r--  1 root root 1460 2005-09-02 09:52 /etc/passwd
    [1] =>
    [2] =>
)
exitcode -1

exitcode 1

Array
(
    [0] => -rw-r--r--  1 root root 1460 2005-09-02 09:52 /etc/passwd
    [1] =>
    [2] =>
    [exitcode] => 0
)
sergey1369 at narod dot ru
29-Aug-2003 07:16
Under PHP/4.3.3RC2, in case of two processes
these function may hangs. Work around is not use
proc_close, or put it after all fcloses done.

For example, this code hangs.

$ph1 = proc_open("cat",
 array(0=>array("pipe","r"),1=>array("pipe","w")),
 $pipes1);
$ph2 = proc_open("cat",
 array(0=>array("pipe","r"),1=>array("pipe","w")),
 $pipes2);

fclose($pipes1[0]); fclose($pipes1[1]); proc_close($ph1);
fclose($pipes2[0]); fclose($pipes2[1]); proc_close($ph2);

This code worked for me:

$ph1 = proc_open("cat",
 array(0=>array("pipe","r"),1=>array("pipe","w")),
 $pipes1);
$ph2 = proc_open("cat",
 array(0=>array("pipe","r"),1=>array("pipe","w")),
 $pipes2);

fclose($pipes1[0]); fclose($pipes1[1]);
fclose($pipes2[0]); fclose($pipes2[1]);
proc_close($ph1); proc_close($ph2);

proc_get_status" width="11" height="7"/> <passthru
Last updated: Fri, 20 Jun 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites