proc_close() - 关闭由proc_open()打开的进程并且返回进程退出码 - php 执行命令函数
proc_close()
(PHP 4 >= 4.3.0, PHP 5, PHP 7)
关闭由proc_open()打开的进程并且返回进程退出码
说明
proc_close(resource $process): intproc_close()同pclose()函数类似,只是proc_close()只能用来关闭由proc_open()函数打开的进程。proc_close()函数会等待进程终止,并且返回进程的返回值。如果有连接到进程的已经打开的管道,那么需要在调用此函数之前调用fclose()函数来关闭管道,否则会引发死锁-在管道处于打开状态时,子进程将不能退出。
参数
$process要关闭的由proc_open()打开的resource。
返回值
返回进程的终止状态码。如果发生错误,将返回-1。
Note:If PHP has been compiled with --enable-sigchild, the return value of this function is undefined.
Regarding: "Returns the termination status of the process that was run. In case of an error then -1 is returned." This is, at best, misleading. It returns: * -1 on error, * WEXITSTATUS(status) if WIFEXITED(status) is true, or * status if WIFEXITED(status) is false, where status is the status parameter of waitpid(). This makes it impossible to differentiate between a relatively normal exit or a termination by signal, and reduces the value of the proc_close return code to a binary one (ok / something broke). This can be seen in proc_open_rsrc_dtor() in ext/standard/proc_open.c (PHP 5.4.44, 5.6.12).
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. 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 )
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"] );
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
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);
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)