Example for tailing a file (like tail -f) using inotify.
<?php
/**
* Tail a file (UNIX only!)
* Watch a file for changes using inotify and return the changed data
*
* @param string $file - filename of the file to be watched
* @param integer $pos - actual position in the file
* @return string
*/
function tail($file,&$pos) {
// get the size of the file
if(!$pos) $pos = filesize($file);
// Open an inotify instance
$fd = inotify_init();
// Watch $file for changes.
$watch_descriptor = inotify_add_watch($fd, $file, IN_ALL_EVENTS);
// Loop forever (breaks are below)
while (true) {
// Read events (inotify_read is blocking!)
$events = inotify_read($fd);
// Loop though the events which occured
foreach ($events as $event=>$evdetails) {
// React on the event type
switch (true) {
// File was modified
case ($evdetails['mask'] & IN_MODIFY):
// Stop watching $file for changes
inotify_rm_watch($fd, $watch_descriptor);
// Close the inotify instance
fclose($fd);
// open the file
$fp = fopen($file,'r');
if (!$fp) return false;
// seek to the last EOF position
fseek($fp,$pos);
// read until EOF
while (!feof($fp)) {
$buf .= fread($fp,8192);
}
// save the new EOF to $pos
$pos = ftell($fp); // (remember: $pos is called by reference)
// close the file pointer
fclose($fp);
// return the new data and leave the function
return $buf;
// be a nice guy and program good code ;-)
break;
// File was moved or deleted
case ($evdetails['mask'] & IN_MOVE):
case ($evdetails['mask'] & IN_MOVE_SELF):
case ($evdetails['mask'] & IN_DELETE):
case ($evdetails['mask'] & IN_DELETE_SELF):
// Stop watching $file for changes
inotify_rm_watch($fd, $watch_descriptor);
// Close the inotify instance
fclose($fd);
// Return a failure
return false;
break;
}
}
}
}
// Use it like that:
$lastpos = 0;
while (true) {
echo tail($file,$lastpos);
}
?>
inotify_init
(PECL inotify >= 0.1.2)
inotify_init — Inicializa una instancia inotify
Descripción
resource inotify_init
( void
)
Inicializar una instancia inotify para usar con inotify_add_watch()
Valores devueltos
Un recurso de secuencia o FALSE en caso de error.
Ejemplos
Ejemplo #1 Ejemplo de uso de inotify
<?php
// Abrir una instancia inotify
$fd = inotify_init();
// Observar __FILE__ por cambios en los metadatos (por ejemplo: mtime)
$watch_descriptor = inotify_add_watch($fd, __FILE__, IN_ATTRIB);
// genera un evento
touch(__FILE__);
// Leer eventos
$eventos = inotify_read($fd);
print_r($eventos);
// Los siguientes métodos permiten utilizar funciones inotify sin bloqueo en inotify_read():
// - Usando stream_select() en $fd:
$read = array($fd);
$write = null;
$except = null;
stream_select($read,$write,$except,0);
// - Usando stream_set_blocking() en $fd
stream_set_blocking($fd, 0);
inotify_read($fd); // No hace ningún bloqueo, y devuelve false si no hay eventos pendientes
// - Usando inotify_queue_len() para comprobar si la lista de eventos no está vacía
$queue_len = inotify_queue_len($fd); // Si > 0, inotify_read() no bloqueará
// Dejar de observar __FILE__ por cambios en los metadatos
inotify_rm_watch($fd, $watch_descriptor);
// Cierra la instancia inotify
// This may have closed all watches if this was not already done
// Esto puede haber cerrado todos los seguimientos, si no se hizo ya
fclose($fd);
?>
El resultado del ejemplo sería algo similar a:
array(
array(
'wd' => 1, // Equivale a $ watch_descriptor
'mask' => 4, // bit IN_ATTRIB establecido
'cookie' => 0, // id único para conectar los eventos relacionados (por ejemplo:
// eventos IN_MOVE_FROM IN_MOVE_TO)
'name' => '', // el nombre de un fichero (por ejemplo: si se monitorean los
// cambios en un directorio)
),
);
Ver también
- inotify_add_watch() - Añade un seguimiento a una instancia inotify inicializada
- inotify_rm_watch() - Elimina un seguimiento existente de una instancia inotify
- inotify_queue_len() - Devuelve un número superior a cero si hay eventos pendientes
- inotify_read() - Lee eventos de una instancia inotify
- fclose() - Cierra un puntero a un archivo abierto
david dot schueler at tel-billig dot de ¶
2 years ago
