forked from arnaud-lb/php-inotify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtail.php
executable file
·65 lines (48 loc) · 1.18 KB
/
tail.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/php
<?php
/**
* Simple tail program using the inotify extension
* Usage: ./tail.php file
*/
function main_loop($file, $file_fd) {
$inotify = inotify_init();
if ($inotify === false) {
fprintf(STDERR, "Failed to obtain an inotify instance\n");
return 1;
}
$watch = inotify_add_watch($inotify, $file, IN_MODIFY);
if ($watch === false) {
fprintf(STDERR, "Failed to watch file '%s'", $file);
return 1;
}
while (($events = inotify_read($inotify)) !== false) {
echo "Event received !\n";
foreach($events as $event) {
if (!($event['mask'] & IN_MODIFY)) continue;
echo stream_get_contents($file_fd);
break;
}
}
// May not happen
inotify_rm_watch($inotify, $watch);
fclose($inotify);
fclose($file_fd);
return 0;
}
ob_implicit_flush(1);
if (!extension_loaded('inotify')) {
fprintf(STDERR, "Inotify extension not loaded !\n");
exit(1);
}
if ($argc < 2) {
fprintf(STDERR, "Usage: " . $argv[0] . " [FILE]\n");
exit(1);
}
$file = $argv[1];
if (!file_exists($file) || ($fd = fopen($file, "r")) === false) {
fprintf(STDERR, "File '%s' does not exists or is not readable\n", $file);
exit(1);
}
fseek($fd, 0, SEEK_END);
exit(main_loop($file, $fd));
?>