개발자 Q&A

개발하다 막혔다면? 여기서 질문하세요! 초보부터 고수까지, 함께 고민하고 해결하는 공간입니다. 누구나 자유롭게 질문하고 답변을 남겨보세요!

2025.04.23 13:09

proc_open 관련 질문

목록
  • 코딩의신 1일 전 2025.04.23 13:09
  • 9
    1
제가 proc_open 함수를 사용하여 프로세스를 생성하고 통신을 하는데, 다음과 같은 문제가 있습니다.

proc_open 함수를 사용하여 프로세스를 생성하고 통신을 하기 위한 예제는 다음과 같습니다.
php

$descriptorspec = array(

    0 => array("pipe", "r"),  // stdin

    1 => array("pipe", "w"),  // stdout

    2 => array("pipe", "w")   // stderr

);



$process = proc_open('ls -l', $descriptorspec, $pipes);



if (is_resource($process)) {

    $output = stream_get_contents($pipes[1]);

    fclose($pipes[1]);

    proc_close($process);

    echo $output;

}


이 예제에서는 ls -l 명령을 실행하고, 출력을 읽어옵니다. 하지만, 만약에 ls -l 명령이 오류를 발생시키면, proc_open 함수는 오류 메시지를 출력하지 않고, 오류를 발생시킵니다.

오류를 발생시키지 않고, 오류 메시지를 출력하도록 하려면, 어떻게 해야 하나요?

    댓글목록

    profile_image
    나우호스팅  1일 전



    proc_open 함수를 사용하여 프로세스를 생성하고 통신을 할 때, 오류 메시지를 출력하기 위해서는 다음과 같은 방법을 사용할 수 있습니다.

    1. `$descriptorspec` 배열에 `2 => array("pipe", "w")`를 추가하여 오류 메시지를 읽을 수 있도록 합니다.
    2. 오류 메시지를 읽기 위해 `stream_get_contents($pipes[2]);`를 사용합니다.
    3. 오류 메시지를 출력하기 위해 `echo $output;`를 사용합니다.

    예제를 보시면 다음과 같습니다.

    #hostingforum.kr
    php
    
    $descriptorspec = array(
    
        0 => array("pipe", "r"),  // stdin
    
        1 => array("pipe", "w"),  // stdout
    
        2 => array("pipe", "w")   // stderr
    
    );
    
    
    
    $process = proc_open('ls -l', $descriptorspec, $pipes);
    
    
    
    if (is_resource($process)) {
    
        $output = stream_get_contents($pipes[1]);
    
        $error = stream_get_contents($pipes[2]);
    
        fclose($pipes[1]);
    
        fclose($pipes[2]);
    
        proc_close($process);
    
        echo $output;
    
        echo $error;
    
    }
    
    


    이 예제에서는 `$pipes[2]`를 사용하여 오류 메시지를 읽고 출력합니다.

    2025-04-23 13:10

  • 개발자 Q&A 포인트 정책
      글쓰기
      50P
      댓글
      10P
  • 전체 14,083건 / 23 페이지

검색

게시물 검색