Last active
April 18, 2016 20:45
-
-
Save kchristensen/375f0cdc3279c146c1b8 to your computer and use it in GitHub Desktop.
Inotify example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$fd = inotify_init(); | |
$watch_dir = '/tmp/'; | |
$watch_email = '[email protected]'; | |
$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