2013-11-27 1 views
0

다른 PHP 스크립트에서 백그라운드 PHP 스크립트를 시작하기 위해 여러 가지 방법을 시도했습니다. 다운로드 할 데이터베이스 파일의 zip 파일을 만들어야합니다. 단일 파일을 즉시 다운로드하도록 설정했지만 파일을 압축해야하는 경우 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'); 

또는

01 심지어 자바 스크립트

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"); 
} 

노력 2,

심지어 아약스 배경으로 않을 것입니다!

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를 호출하면 현재 스크립트의 실행을 중단합니다 (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

나는 출력을 파일로 리디렉션하려고 않았다,하지만 여전히 중단됩니다. 윈도우와 함께 exec를 사용하는 것에 대한 여러 리뷰를 들었습니다. 어떤 사람들은 전혀 효과가 없다고 말하는 사람들도 있고 그렇지 않은 사람들도 있습니다. 그러나 내가 시도하고있는 방법 중 어느 것도 (매달려있는 문제로) 작동하지 않습니다. – Monkeybutt

+0

그렇다면 문제의 원인을 너무 확신하지 못합니다. 리눅스 개발 환경에 액세스 할 수 있다면 스크립트가 리눅스 환경에서 올바르게 실행되는지 확인할 수 있습니까? (또는 당신이 리눅스 배포판을 다운로드해서 하나를 설치할 수 없다면) – echochamber

+0

리눅스 환경에서 테스트해볼 것이다. 액세스 권한이 없으므로 직접 설정해 주셔서 감사합니다.나는'exec (sprintf ('/ usr/bin/php ...') 메소드도 성공하지 못했습니다. – Monkeybutt

관련 문제