2013-01-02 1 views
2

사용자 작업 후에 서버에서 백그라운드로 실행되고 싶은 PHP 스크립트가 있습니다. 명령은 백그라운드에서 실행되어야하는 반면 사용자는 다른 페이지로 리디렉션되어야합니다. 다음은 위의 스크립트가 잘 실행되는 코드PHP exec 및 header redirect

$command = exec('php -q /mylongrunningscript.php'); 
header("Location: /main.php?action=welcome"); 

이지만, $command = exec('php -q /mylongrunningscript.php');이 실행될 때까지 페이지가 리디렉션되지 않습니다.

해당 사용자가 즉시 환영 페이지로 리디렉션되도록하고 싶습니다.

이 작업을 수행하는 다른 방법이 있습니까? 다른 아이디어는 그 $ command = exec ('php -q /mylongrunningscript.php'); 환영 페이지에서 실행되어야하지만 환영 페이지 HTML은 명령이 실행 된 후에 표시됩니다. 명령은 약 5,6 분이 소요되며 이번에는 페이지가 리디렉션되지 않습니다.

내가 PHP와 센트 OS 리눅스에서 오전 5.3

답변

3

는 대신이 시도 할 수 :

$result = shell_exec('php -q /mylongrunningscript.php > /dev/null 2>&1 &'); 

PS를 : 당신이 다음 사용 출력을 캡처하려면이 /dev/null에 표준 출력 및 표준 오류 리디렉션되어 있습니다 :

$result = shell_exec('php -q /mylongrunningscript.php > /tmp/script.our 2>&1 &'); 

는 또한 백그라운드에서 유닉스 명령을 실행하려면이 PHP 함수를 사용

//Run linux command in background and return the PID created by the OS 
function run_in_background($Command, $Priority = 0) { 
    if($Priority) 
     $PID = shell_exec("nohup nice -n $Priority $Command > /dev/null & echo $!"); 
    else 
     $PID = shell_exec("nohup $Command > /dev/null & echo $!"); 
    return($PID); 
} 

씨 : 주석은에 스크립트 출력을 보내기

2

반환합니다

이 기능을 사용하여 프로그램을 시작한 경우 을 백그라운드에서 계속 실행하려면 프로그램은 이 파일이나 다른 출력 스트림으로 리디렉션되어야합니다. 그렇게하지 않으면 은 프로그램 실행이 끝날 때까지 PHP가 멈추도록합니다. 그래서

사용의 그렇게하자 2>&1 (기본적으로 2 stderr 1은 stdout, 그래서 이것이 의미하는 무엇이다 "리디렉션을 stdout에 모든 표준 에러 메시지") :

shell_exec('php -q /mylongrunningscript.php 2>&1'); 

또는 경우 그것은 출력 알고 싶어 :

shell_exec('php -q /mylongrunningscript.php 2>&1 > output.log'); 
+0

을 어떻게 내가이 = mylongrunningscript.php 및 UID처럼 mylongrunningscript.php에 인수를 전달할 수있는 12 – Asghar

+0

@Asghar'GET' 매개 변수는 HTTP 프로토콜 만 사용하기 때문에 stdin을 읽을 필요가 있습니다 ('$ _SERVER [ "argv"]'를 확인하십시오). 대신'curl http://mywebsite.com/mylongrunningscript.php?my&get¶ms=here 2> & 1'과 같은 방법으로 HTTP 프로토콜을 사용할 수 있습니다. – h2ooooooo