开发者

Graceful DOS Command Error-Handling

开发者 https://www.devze.com 2022-12-25 13:17 出处:网络
PHP 5.2.13 on Windows 2003 I am using the DOS Start /B command to launch a background application using the PHP popen() function:

PHP 5.2.13 on Windows 2003

I am using the DOS Start /B command to launch a background application using the PHP popen() function:

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

The popen() function launches a cmd.exe process that runs the specified command; however, if the command fails (e.g. the {$app}.exe doesn't exist or is locked in the above example), the cmd.exe process never returns, and PHP hangs indefinitely as a result.

Calling the failing DOS command directly using the Command Prompt results in an Error prompt that requires clicking the OK but开发者_如何学Pythonton.

I assume this error confirmation requirement is what's preventing the cmd.exe process from returning to PHP both from the Command Prompt (using both CGI and CLI) and the web (using Apache 2.0 handler w/Apache 2.2).

Is there a way to write the DOS command or configure the server or cmd.exe app itself to return the DOS error to the originating call rather than waiting for confirmation?


From the manual: 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);


Have you tried cmd.exe /c ... instead of start /b?

0

精彩评论

暂无评论...
验证码 换一张
取 消