2013-11-27 100 views
0

我已经尝试过使用NUMEROUS方法从另一个php脚本启动后台php脚本。我需要创建一个数据库文件的zip文件供下载。我已经设置了立即下载单个文件的功能,但是如果需要压缩文件,我需要启动一个后台进程,将链接发送到zip文件。该过程有效,但原始脚本仍然等待该过程完成。我尝试了以下方法,就像我说的那样,他们工作,但他们不会去背景。它让我等待了超过8分钟的特定实例(大文件压缩),并且没有用户会等待。PHP - Windows后台命令正在工作,但仍然在等待后台进程完成

print `php -f zipfiles7.php | at now`; 

$WshShell = new COM("WScript.Shell"); 
$oExec = $WshShell->Run("C:\wamp\bin\php\php5.4.3\php.exe -f C:/wamp/www/zipfiles7.php", 0, false); 

$WshShell = new COM("WScript.Shell"); 
$oExec = $WshShell->Run("cmd /c C:\wamp\bin\php\php5.4.3\php.exe -f C:/wamp/www/zipfiles7.php", 3, false); 

$exe = "C:\wamp\bin\php\php5.4.3\php.exe"; 
$args = "C:\wamp\www\zipfiles7.php"; 
pclose(popen("start \"bla\" \"" . $exe . "\" " . escapeshellarg($args), "r")); 

$cmd = "php-win.exe zipfiles7.php"; 
$cmd = "start /B php-win.exe zipfiles7.php"; 
$PID = shell_exec("nohup $cmd > test.txt 2>&1"); 

exec("Psexec.exe -i -d php.exe zipfiles7.php"); 

$cmd = "C:\wamp\bin\php\php5.4.3\php-win.exe -f C:\wamp\www\zipfiles7.php"; 
function _exec($cmd) 
{ 
    $WshShell = new COM("WScript.Shell"); 
    $oExec = $WshShell->Run($cmd, 0,false); 
    echo $cmd; 
    return $oExec == 0 ? true : false; 
}_exec("C:\wamp\bin\php\php5.4.3\php-win.exe -f C:\wamp\www\zipfiles7.php"); 

$cmd = "C:\wamp\bin\php\php5.4.3\php-win.exe -f C:\wamp\www\zipfiles7.php"; 
pclose(popen('start /B ' . $cmd, 'r')); 

$cmd = "C:\wamp\bin\php\php5.4.3\php-win.exe -f C:\wamp\www\zipfiles7.php"; 
pclose(popen("start /B " . $cmd, "r")); 

system("cmd /c php zipfiles7.php > test.txt 2>&1"); 

exec('psexec start /B -d php.exe zipfiles7.php > test.txt 2>&1'); 

exec('psexec -d php.exe zipfiles7.php > test.txt 2>&1'); 

exec('psexec -d php.exe zipfiles7.php'); 

$cmd = "C:\wamp\bin\php\php5.4.3\php-win.exe -f C:\wamp\www\zipfiles7.php"; 
exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, "test.txt", "php_error.log")); 

pclose(popen('start /B php zipfiles7.php > test.txt 2>&1', 'r')); 

pclose(popen("start /B C:\wamp\bin\php\php5.4.3\php-win.exe -f C:\wamp\www\zipfiles7.php", "r")); 

shell_exec('start /B "C:\wamp\bin\php\php5.4.3\php.exe -f C:\wamp\www\zipfiles7.php > test.txt 2>&1"'); 

exec('START /B "C:\wamp\bin\php\php5.4.3\php.exe -f C:\wamp\www\zipfiles7.php"', $output); 

passthru("start /B c:/wamp/bin/php/php5.4.3/php-win c:/wamp/www/zipfiles7.php > test.txt 2>&1"); 

exec("php.exe -f zipfiles7.php > test.txt 2>&1"); 

exec("php-win -f zipfiles7.php > /dell/null 2>/dev/null &", $output); 

exec("start /B php zipfiles7.php > test.txt 2>&1"); 

exec("start php.exe -f zipfiles7.php > test.txt 2>&1"); 

$WshShell = new COM("WScript.Shell"); 
$oExec = $WshShell->Run("cmd /C C:\wamp\www\batch.bat", 0, false); 

exec("CHP.exe C:\wamp\www\batch.bat > test.txt 2>&1"); 

exec("start /min php zipfiles7.php > test.txt 2>&1"); 

shell_exec('call "cmd /c batch.bat"'); 

batch.bat

@echo off 
"start /B bg C:\wamp\bin\php\php5.4.3\php.exe -f C:\wamp\www\zipfiles7.php > test.txt" 

的另外的方法的尝试:

$descriptorspec = array(
     0 => array("pipe", "r"), // stdin is a pipe that the child will read from 
     1 => array("pipe", "w"), // stdout is a pipe that the child will write to 
     2 => array("pipe", "r") // stderr is a file to write to 
    ); 

    $cmd = "start /B php.exe zipfiles7.php"; 
    $process = proc_open($cmd, $descriptorspec, $pipes, null, null); 
    if (is_resource($process)) 
    { .... 

// buffer all upcoming output 
ob_start(); 
// get the size of the output 
$size = ob_get_length(); 

// send headers to tell the browser to close the connection 
header("Content-Length: $size"); 
header('Connection: close'); 

// flush all output 
ob_end_flush(); 
ob_flush(); 
flush(); 

// close current session 
if (session_id()) session_write_close(); 
    exec("start /B php zipfiles7.php > test.txt 2>&1"); 

shell_exec('psexec -s -d php.exe zipfiles7.php'); 

$runCommand = 'php -q zipfiles7.php'; 
    $WshShell = new COM(“WScript.Shell”); 
    $oExec = $WshShell->Run($runCommand, 7, false); 

即使想的JavaScript

function background(){ 
var url = "C:\wamp\www\zipfiles7.php";// No question mark needed 

xmlReq=new XMLHttpRequest(); 

xmlReq.open("POST",url,true); 
xmlReq.send(); 
console.log("done"); 
} 
echo '<script type="text/javascript">', 'background();', '</script>'; 

function background(){ 
var str = "Success"; 
var url = "C:\wamp\www\zipfiles7.php"; 

xmlReq=new XMLHttpRequest(); 

xmlReq.open("POST",url,true); 
xmlReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
xmlReq.setRequestHeader("Content-length", str.length); 
xmlReq.setRequestHeader("Connection", "close"); 
xmlReq.send(); 
console.log("done"); 
} 

即使阿贾克斯是不会的背景!

function background() 
{ 
$.ajax(
    { 
      type: "POST", 
      url: "zipfiles7.php", 
      data: data, // data to send to above script page if any 
      cache: false, 

      success: function(response) 
      { 
       alert('Succes'); 
      } 
    }); 
} 

请帮我理解为什么这个过程不会进入后台。先谢谢你。

回答

0

当你调用exec php暂停当前脚本的执行,直到它从shell获得一个返回值(来自exec)。所以在这种情况下,在文件完成压缩之前,php将不会继续执行脚本。

From the php exec() documentation page:

如果程序开始使用此功能,以便它 继续在后台运行,该程序的输出必须 重定向到文件或其他输出流。如果不这样做, 会导致PHP挂起,直到程序执行结束。

就实施这种正确的方式而言,我实际上从来没有必须自己做这件事,所以我不能完全说清楚,但我希望这能指出您的方向是正确的。

编辑:Upon searching a little further I found this stackoverflow answer which seems like it applies to your current question.

+0

我曾尝试将输出重定向到一个文件,但它仍然挂起。我听说有关在Windows上使用exec的综合评论。有人说它根本不起作用,有些人不同意。但是我所尝试的方法都没有工作(与悬而未决的问题)。 – Monkeybutt

+0

然后我不太确定造成这个问题的原因。如果你有权访问Linux开发环境,你可以看到你的脚本是否在linux环境下正确执行? (或者如果你不能,你可以下载一些Linux发行版并设置一个) – echochamber

+0

我将研究在Linux环境下的测试。我无法访问,所以谢谢你提出建立我自己的建议。我确实尝试过'exec(sprintf('/ usr/bin/php ...')方法,但没有成功。 – Monkeybutt