2011-04-29 3 views
1

나는 데이터베이스 백업하기 위해 다음 명령을 실행 mysqldump이 실패하면PHP : 실패한 쉘 명령의 오류 메시지를받는 방법?

$exec = exec("mysqldump --opt 
         --user=$database_user 
         --password=$database_pass 
         --host=$database_host 
         $database_name > $output_filename", $out, $status); 

확인하기를 내가 수행하는 경우 뭔가에

if ($status == 0) { 
    // OK 
} else { 
    // Error 
    // How could I print the error message here ? 
} 

은 잘못하고 mysqldump 내가 오류를 얻을 수있는 방법 실패 메시지 ?

답변

2

proc_open (Emil이 제안한 내용)을 사용할 수 있습니다. 아래는 여러분이 원하는 것을 성취 할 수있는 방법에 대한 좀 더 완벽한 예입니다.

$exec_command = "mysqldump --opt 
        --user=$database_user 
        --password=$database_pass 
        --host=$database_host 
        $database_name" 

$descriptorspec = array(
         0 => array("pipe", "r"), // stdin pipe 
         1 => array("file", $output_filename, "w"), // stdout to file 
         2 => array("pipe", "w") // stderr pipe 
); 

$proc = proc_open($exec_command, $descriptorspec, $pipes); 

fwrite($pipes[0], $input); //writing to std_in 
fclose($pipes[0]); 

$err_string = stream_get_contents($pipes[2]); //reading from std_err 
fclose($pipes[2]); 

$return_val = proc_close($proc); 

편집 :

변경된 출력은 shell_exec를 사용하는 경우

+0

고마워요 !!! –

1

stderr를 읽으려면 proc_open을 사용해야합니다. 설명서의 예를 참조하십시오.

+0

또는 stdout으로 리디렉션하고'$ output_filename'에서 추출하십시오. 덤프는 오류 발생시 유효하지 않을 수 있기 때문에. –

+0

@Marc : 어떻게해야합니까? 명령을 보여 주시겠습니까? –

+0

'2> & 1'은 stderr (파일 핸들 2 번)를 stdout (파일 핸들 # 1)로 리디렉션합니다. –

0

을 파일에 쓸 수있는 명령 2> & 1을 추가, 그것은 STDOUT에 STDERR을 리디렉션합니다.

관련 문제