我的猜測是,你所要做的是不能直接。通過後臺進程,您可以讓PHP腳本在存在結果之前繼續(並可能退出)。
解決辦法是有第二個PHP(或Bash/etc)腳本執行命令並將結果寫入臨時文件。
主要腳本會是這樣的:
$resultFile = '/tmp/result001';
touch($resultFile);
exec('php command_runner.php '.escapeshellarg($resultFile).' > /dev/null 2>&1 &');
// do other stuff...
// Sometime later when you want to check the result...
while (!strlen(file_get_contents($resultFile))) {
sleep(5);
}
$result = intval(file_get_contents($resultFile));
unlink($resultFile);
而且command_runner.php
會是什麼樣子:
$outputFile = $argv[0];
exec('badcommand > /dev/null 2>&1', $output, $result);
file_put_contents($outputFile, $result);
它不漂亮,肯定有餘地增加的魯棒性和處理併發執行,但總體思路應該起作用。
非常感謝您的回答,非常有幫助。 – Brian 2009-10-07 20:08:41