Question

The following script monitors the folder /dev/shm/test and outputs any created file in real time:

<?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
);

$cwd = '/tmp';
$env = array('some_option' => 'aeiou');

$process = proc_open('inotifywait -mc -e create /dev/shm/test/', $descriptorspec, $pipes, $cwd, $env);

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

The problem is that it never ends, runs forever. Any clue on why could this be happening? I have max_execution_time = 30 in php.ini

How can I monitor how many resources this script uses?

Thanks a lot.

Was it helpful?

Solution

max_execution_time defines the real time your script is allowed to run, so this does not includes any time spent outside of the script execution, even if it was triggered by it: database queries, filesystem polling, network, etc.

Solution is to use inotifywait's -t <seconds>, --timeout <seconds> option.

OTHER TIPS

max_execution_time affects online execution only. CLI-based scripts run forever

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top