The Inotify class provides methods to manage file system watches.
new Inotify([persistent]): Creates a new instance. Defaults to persistent mode. Pass false for non-persistent mode.addWatch(arg): Adds a watch for a file or directory. Returns a watch descriptor (integer).arg.path: Path to monitor.arg.watch_for: An OR'ed set of events (e.g., Inotify.IN_ALL_EVENTS). Defaults to Inotify.IN_ALL_EVENTS.arg.callback: Function receiving the event object.
removeWatch(watch_descriptor): Removes a watch. Returns true if successful, false otherwise. Removing a watch triggers an Inotify.IN_IGNORED event.close(): Removes all watches and closes the inotify file descriptor. Returns true if successful, false otherwise.
Note: Monitoring of directories is not recursive. To monitor subdirectories, you must create additional watches for each subdirectory.
var Inotify = require('inotify').Inotify;
var inotify = new Inotify(); // persistent by default
var wd = inotify.addWatch({
path: '.',
watch_for: Inotify.IN_ALL_EVENTS,
callback: function (event) {
console.log('Event received:', event);
}
});
// Later, remove the watch
inotify.removeWatch(wd);