Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monitor events in a filesystem as they happen

I want to to write a script in either Ruby or Python that will look at the contents of a directory and monitor for

1. Any new files
2. Any saves to existing files. 

I'm not bothered about what is different in the files - just that they have been saved or are new.

The script then returns the name of the file (with its full path) perhaps once every 2 seconds or something.

I want to scan files within folders to so

mainfile.txt
images/image1.jpg
images/icons/icon1.png

All these files / folders need to be montitored.

I eventually want to FTP the files that have changed / been added but I'm happy to settle for a solution to the first part of the problem as I know how to ftp (but by all means include this if you want to!)

I am using OSX 10.5.8

like image 600
32423hjh32423 Avatar asked Jun 28 '11 08:06

32423hjh32423


People also ask

How do I monitor a file system event in Python?

watchdog is an open-source python API library that is a cross-platform API to monitor file system events. You can specify a folder or a directory to watchdog observer, which keeps monitoring the folder for any changes like file creation, modification, deletion, or moving of files from one folder to another.

What is inotify used for?

inotify (inode notify) is a Linux kernel subsystem created by John McCutchan, which monitors changes to the filesystem, and reports those changes to applications. It can be used to automatically update directory views, reload configuration files, log changes, backup, synchronize, and upload.

Does inotify work with NFS?

The inotify interface does have limitations—it can't monitor remote, network-mounted filesystems (that is, NFS); it does not report the userid involved in the event; it does not work with /proc or other pseudo-filesystems; and mmap() operations do not trigger it, among other concerns.

What is inotify tool Linux?

inotify (short for inode notify) is a Linux kernel subsystem that notices changes in a file system (file/directory) and notifies those changes to applications.


2 Answers

For ruby here is a gem which can help you: https://github.com/thibaudgg/rb-fsevent

FSEvent is the system you want to use on OS X and this is a binding bringing it to Ruby, this gem is used by Guard that is how I found it. (https://github.com/guard/guard)

You could even consider using Guard which provide a nice api to monitor files/paths and run action on changes.

Edit: Since I posted this a new gem was released to abstract platform specific stuff: https://github.com/guard/listen , this gem provide a portable api to monitor filesystem events on multiple platforms

like image 162
Schmurfy Avatar answered Oct 02 '22 16:10

Schmurfy


Ah, watching FTP. As it happens, I built something like this for work. While mine was dealing with several constraints I hope you don't have, the part that sat on linux was easy. OSX in this day and age is mostly a repackaged linux.

So, you should be able to install iwatch, which uses the inotify kernel feature. If you can't use iwatch itself, which would be very surprising as it's written in perl, then the source should point you to how to directly communicate with inotify. This link has stuff on using python with inotify.

There are a few other alternatives to iwatch, but they all hook into inotify. The only other alternative is to do manual directory scans on a timer, which is clunky and should be avoided. I did it on windows, and it has.... problems. If I ever get the chance, I'll be reworking that section.

like image 34
Spencer Rathbun Avatar answered Oct 02 '22 17:10

Spencer Rathbun