PHP Conference Japan 2024

proc_close

(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)

proc_close關閉由 proc_open() 開啟的程序,並傳回該程序的結束碼

描述

proc_close(resource $process): int

proc_close() 類似於 pclose(),但它僅適用於由 proc_open() 開啟的程序。proc_close() 會等待程序終止,並傳回其結束碼。當呼叫此函式時,為了避免死鎖,會關閉到該程序的開啟管道 - 子程序可能在管道開啟時無法結束。

參數

process

將被關閉的 proc_open() 資源

傳回值

傳回已執行程序的終止狀態。若發生錯誤,則傳回 -1

注意:

如果 PHP 編譯時使用了 --enable-sigchild,則此函式的傳回值未定義。

新增註解

使用者貢獻註解 5 則註解

13
oohay251 at yahoo dot com
19 年前
根據網路上各式各樣的文章和最近的經驗,我觀察到您不能依賴 proc_close 傳回子程序準確的傳回碼。傳回碼也取決於您是否從 stdout/stderr 管道讀取,如我的範例所示。我透過將結束碼寫入額外的檔案描述器來解決這個問題。

<?
$descriptorspec = array(
0 => array('pipe', 'r'), // stdin 是一個管道,子程序將從中讀取
1 => array('pipe', 'w'), // stdout 是一個管道,子程序將寫入其中
2 => array('pipe', 'w'), // stderr 是一個管道,子程序將寫入其中
);
$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 是一個管道,子程序將從中讀取
1 => array('pipe', 'w'), // stdout 是一個管道,子程序將寫入其中
2 => array('pipe', 'w'), // stderr 是一個管道,子程序將寫入其中
);
$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 是一個管道,子程序將從中讀取
1 => array('pipe', 'w'), // stdout 是一個管道,子程序將寫入其中
2 => array('pipe', 'w'), // stderr 是一個管道,子程序將寫入其中
3 => array('pipe', 'w'), // stderr 是一個管道,子程序將寫入其中
);
$proc = @proc_open("/bin/ls -l /etc/passwd;echo $? >&3", $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]);
if (!feof($pipes[3])) $output['exitcode']=rtrim(fgets($pipes[3],5),"\n");
fclose($pipes[3]);
proc_close($proc);
print_r($output);
?>

在我系統上的輸出

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

exitcode 1

陣列
(
[0] => -rw-r--r-- 1 root root 1460 2005-09-02 09:52 /etc/passwd
[1] =>
[2] =>
[exitcode] => 0
)
9
Uwe Ohse
9 年前
關於:「傳回已執行程序的終止狀態。若發生錯誤,則傳回 -1。」

這頂多算是誤導。它會傳回
* 錯誤時傳回 -1,
* 如果 WIFEXITED(status) 為 true,則傳回 WEXITSTATUS(status),或
* 如果 WIFEXITED(status) 為 false,則傳回 status,
其中 status 是 waitpid() 的 status 參數。

這使得無法區分相對正常的結束或訊號終止,並將 proc_close 傳回碼的值降低為二進制 (ok / 某些東西壞了)。

這可以在 ext/standard/proc_open.c 中的 proc_open_rsrc_dtor() 中看到 (PHP 5.4.44, 5.6.12)。
1
ashnazg at php dot net
17 年前
似乎如果您在編譯 PHP 時設定了 --enable-sigchild (根據我的理解,這是您使用 Oracle 程式碼所必需的),則無法信任來自 proc_close() 的傳回碼。

在我擁有的 PHP4 (4.4.7) 和 PHP5 (5.2.4) 版本上使用 proc_open 的範例 1998 程式碼,傳回碼始終為 "-1"。這也是我執行其他 shell 命令 (無論它們成功或失敗) 時可以造成的唯一傳回碼。

除了在這個舊的錯誤報告中,我沒有在任何地方看到此警告 -- http://bugs.php.net/bug.php?id=29123
1
sergey1369 at narod dot ru
21 年前
在 PHP/4.3.3RC2 中,如果兩個程序
這些函式可能會掛起。解決方法是不使用
proc_close,或在完成所有 fcloses 之後放置它。

例如,此程式碼會掛起。

$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);

此程式碼對我有效

$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);
0
morrisdavidd at gmail dot com
16 年前
考慮以下虛擬碼

$SOME_PROCESS = proc_open(/* 這裡放些東西 */);
...
$status = proc_get_status($SOME_PROCESS);
...
$exitCode = proc_close($SOME_PROCESS);

如果外部程式在呼叫 proc_get_status 之前自行結束,則 $exitCode == -1

因此,請考慮使用
$actualExitCode = ($status["running"] ? $exitCode : $status["exitcode"] );
To Top