views:

355

answers:

2

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 button.

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?

A: 

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);
thetaiko
Thanks for the suggestion, but this does not force DOS to return to PHP--it only captures the info once DOS does return.
Captain Obvious
A: 

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

ewall