다음 스크립트는 /dev/shm/test
에 새 파일을 모니터링하고 이에 대한 정보를 실시간으로 출력합니다.proc_open 좀비 프로세스가 수행
사용자가 브라우저를 닫을 때 inotifywait
프로세스가 열린 상태로 유지되는 등의 문제가 있습니다.
이를 방지 할 수있는 방법이 있습니까?
<?php
$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", "a") // stderr is a file to write to
);
$process = proc_open('inotifywait -mc -e create /dev/shm/test/', $descriptorspec, $pipes);
if (is_resource($process)) {
header("Content-type: text/html;charset=utf-8;");
ob_end_flush(); //ends the automatic ob started by PHP
while ($s = fgets($pipes[1])) {
print $s;
flush();
}
fclose($pipes[1]);
fclose($pipes[0]);
fclose($pipes[2]);
// It is important that you close any pipes before calling
// proc_close in order to avoid a deadlock
$return_value = proc_close($process);
echo "command returned $return_value\n";
}
?>
그럼 코드가 무한 루프를 포함 사용할 수 있습니다. 왜 당신은 그것이 끝없는 반복을하지 않을 것이라고 기대합니까? 또한 : 어떤 SAPI를 사용하고 있습니까? 예를 들어 CLI에서 해당 스크립트를 시작하고 Ctrl + C로 중단하면 어떻게됩니까? "아동 과정"이 살해 되었습니까? 'proc_open'은 자식 프로세스 또는 독립적 인 프로세스를 오픈합니까? 'fgets'로 시간 제한은 무엇입니까? STDOUT에서 읽지 않아도되는 것이 있다면 얼마나 오래 기다려야합니까? – hakre
@hakre 콘솔 모드에서 Ctrl + C를 사용하여 inotifywait 프로세스가 종료되었음을 확인할 수 있습니다.fgets는 Jon의 대답처럼'connection_aborted()'와'proc_terminate()'를 검사하려고 시도했기 때문에 시간 초과가 없었습니다. PHP는 출력을 시도한 후에 만 연결이 중단되었다는 것을 알게되므로 이상하게 여겨집니다. 뭔가 (나는). 또한 이러한 함수를 확인하지 않고도 스크립트는 윈도우로드를 취소 할 때 종료되어야합니다. 그렇습니까? – JorgeeFG
사용중인 PHP 버전은 무엇입니까? – Omiga