php proc close,PHP proc_close 用法 手册 | 示例代码

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 $exitnn";

$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 $exitnn";

$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

)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值