1 2 3 4 5 6 7 8 9 10 |
wget http://pecl.php.net/get/inotify-0.1.6.tgz tar -zxvf ./inotify-0.1.6.tgz cd inotify-0.1.6 phpize ./configure --with-php-config=/usr/local/php/bin/php-config make make install //在php.ini中加入extension=inotify.so /etc/init.d/nginx restart /etc/init.d/php-fpm restart |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php $file = '/tmp/test.txt'; $fd = inotify_init(); //IN_MODIFY表示当文件内容被修改时候 具体参数可参靠php.net $watch_descriptor = inotify_add_watch($fd, $file, IN_MODIFY); if (!$watch_descriptor) { echo 'nothing to modify';exit; } $handle = fopen($file, 'r'); //把文件指针移动到文件底部,防止脚本中断后再次开启监听会读取整个文件 fseek($handle, 0, SEEK_END); while(true){ $event = inotify_read($fd); if ($event) { while ($line = stream_get_line($handle, 4096, "\n")) { //do someing... insert into database | push queue | data handle with storm echo $line . "\n"; } } } inotify_rm_watch($fd, $watch_descriptor); fclose($handle); fclose($fd); |