php - 优美的DOS命令错误处理

标签 php windows error-handling dos

Windows 2003上的PHP 5.2.13

我正在使用DOS Start/B命令来使用PHP popen()函数启动后台应用程序:

popen("start /B {$_SERVER['HOMEPATH']}/{$app}.exe > {$_SERVER['HOMEPATH']}/bg_output.log 2>&1 & echo $!", 'r');

popen()函数启动一个cmd.exe进程,该进程运行指定的命令;但是,如果命令失败(例如,在上述示例中{$ app} .exe不存在或被锁定),则cmd.exe进程将永远不会返回,并且PHP将无限期挂起。

直接使用命令提示符调用失败的DOS命令会导致错误提示,需要单击“确定”按钮。

我认为此错误确认要求是阻止cmd.exe进程从命令提示符(使用CGI和CLI)和Web(使用带Apache 2.2的Apache 2.0处理程序)返回到PHP的原因。

有没有一种方法可以编写DOS命令或配置服务器或cmd.exe应用程序本身,以将DOS错误返回到原始调用而不是等待确认?

最佳答案

从手册:http://php.net/manual/en/function.popen.php

If the command to be executed could not be found, a valid resource is returned. This may seem odd, but makes sense; it allows you to access any error message returned by the shell:


error_reporting(E_ALL);

/* Add redirection so we can get stderr. */
$handle = popen('/path/to/executable 2>&1', 'r');
echo "'$handle'; " . gettype($handle) . "\n";
$read = fread($handle, 2096);
echo $read;
pclose($handle);

关于php - 优美的DOS命令错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2580897/

相关文章:

php - 从隐藏的 url 获取数据

php - yii2 GridView 在表外搜索字段

php - Laravel 5.1 - 检查日期是否过期

sql - 消息 203,级别 16,状态 2,不是有效标识符

java - 如何将字符串从 Android 发送到 PHP?我的 PHP 脚本应该如何处理它?

c++ - 使用 CSplitterWindowImpl 处理移动并使子 Pane 无效

c# - 以编程方式更改某些 Windows 用户名(重命名 Windows 用户)

linux - 当我尝试运行从 Windows 系统编写的 Linux 脚本时出现 "No such file or directory"错误

error-handling - 抑制整个脚本的错误

error-handling - ASC/ASCQ 号码的 Sense Key