Tip: inotify cron

Dear members, moderators and others.

While working on <insert project>, a need has surfaced to watch a directory, and when a file comes, to do appropriate action.
So, i started writing some shell code, aware of linux inotify-tools package with inotifywait .

Also, i'm seeing a lot of similar request on these forums for such kind of actions.

Since linux kernel inotify subsystem is used extensively in the wild (for instance ClamAV), was sure that there was a cleaner way, not resorting to high level language programming

While surfing, i found a neat way to minimize the code used, tool called incrond
Same syntax as cron , minimal script to do action on received input.
The tool is available in EPEL repositories for CentOS, and standard ubuntu / debian ones.

So instead of monster shell code i ended up with a simple incrontab entry and a simple small shell script.

Keep it simple, stupid :

/directory/to/watch	IN_CLOSE_WRITE,IN_MOVED_TO   /path/to/script/script.sh $@ $#

So any file which gets closed (not necessarily written to) or moved to directory being watched will be processed by script.

From documentation :

Hopefully this will help you in your projects with same requirements and save time.

Regards
Peasant.

9 Likes

I checked Fedora's repository. The package is called " incron "
Thanks