PHP: proc_close - Manual

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

search for in the

proc_get_status passthru

Last updated: Fri, 26 Jan 2007

view this page in

# proc_close

(PHP 4 >= 4.3.0, PHP 5)

proc_close -- Close a process opened by proc_open() and return the exit code of that process.

## Описание

int proc_close ( resource process )

proc_close() is similar to pclose() except that it only works on processes opened by proc_open(). proc_close() waits for the process to terminate, and returns its exit code. If you have open pipes to that process, you should fclose() them prior to calling this function in order to avoid a deadlock - the child process may not be able to exit while the pipes are open.

## Список параметров

`process`

  The proc_open() resource that will be closed.

add a note User Contributed Notes
proc_close
e-t172 at e-t172 dot net
18-Oct-2005 10: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 07: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 03: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);`
add a note
proc_get_status passthru

Last updated: Fri, 26 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 10:36:09 2007 PST