Skip to content

Instantly share code, notes, and snippets.

@kchristensen
Last active April 18, 2016 20:45
Show Gist options
  • Save kchristensen/375f0cdc3279c146c1b8 to your computer and use it in GitHub Desktop.
Save kchristensen/375f0cdc3279c146c1b8 to your computer and use it in GitHub Desktop.
Inotify example
<?php
$fd = inotify_init();
$watch_dir = '/tmp/';
$watch_email = 'kyle@crowdgather.com';
$new_files = array();
$last_event = time();
$interval = 10;
stream_set_blocking($fd, 0);
$watch_descriptor = inotify_add_watch($fd, $watch_dir, IN_CREATE);
while (true) {
$events = inotify_read($fd);
if (is_array($events) && preg_match("/.*\.php$/", $events[0]['name'])) {
$new_files[] = $events[0]['name'];
}
if (time() - $last_event > $interval) {
print "Checking for new files...\n";
$last_event = time();
if (count($new_files)) {
printf("Found %s new PHP files on %s, sending email to %s\n", count($new_files), $watch_dir, $watch_email);
mail($watch_email, "New files found on $watch_dir", print_r($new_files, true));
$new_files = array();
}
}
}
inotify_rm_watch($fd, $watch_descriptor);
fclose($fd);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment