update page now
Laravel Live Japan

Voting

: min(two, eight)?
(Example: nine)

The Note You're Voting On

weirdall at hotmail dot com
8 years ago
This script will tail a file using tail -F to follow scripts that are rotated.

<?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", "w")   // stderr is a pipe that stdout will to write to
);

$filename = '/var/log/nginx/nginx-access.log';
if( !file_exists( $filename ) ) {
    file_put_contents($filename, '');
}
$process = proc_open('tail -F /var/log/nginx/stats.bluebillywig.com-access.log', $descriptorspec, $pipes);

if (is_resource($process)) {
    // $pipes now looks like this:
    // 0 => writeable handle connected to child stdin
    // 1 => readable handle connected to child stdout
    // Any error output will be sent to $pipes[2]

    // Closing $pipes[0] because we don't need it
    fclose( $pipes[0] );

    // stderr should not block, because that blocks the tail process
    stream_set_blocking($pipes[2], 0);
    $count=0;
    $stream = $pipes[1];

    while ( ($buf = fgets($stream,4096)) ) {
        print_r($buf);
        // Read stderr to see if anything goes wrong
        $stderr = fread($pipes[2], 4096);
        if( !empty( $stderr ) ) {
            print( 'log: ' . $stderr );
        }
    }
    fclose($pipes[1]);
    fclose($pipes[2]);

    // It is important that you close any pipes before calling
    // proc_close in order to avoid a deadlock
    proc_close($process);
}
?>

<< Back to user notes page

To Top