monitor a folder tree for changes and run a script when a file is created - linux

linux, shell

Solution

`inotify` is being around for a while, It's stable and is part of main stream of many distros.

How to install:

on Ubuntu:

`sudo apt-get install inotify-tools`

on Centos/RHEL (from EPEL repo):

`yum --enablerepo epel install inotify-tools`

How to use:

inotifywait -re create /tmp/test1/ && echo "Change detected"

Once you create a file `echo "change detected"` will be triggered which could be just about anything.

The output you get:

Setting up watches.  Beware: since -r was given, this may take a while!
Watches established.
/tmp/test1/test2/test3/ CREATE file
Change detected

PS. `-r` for recursive `-e create` to detect new files.

Problem

I am trying to create a background running shell script that will notice when a file is created in a filetree, check to see if the filename matches specific criteria, email the file, then move it. The application - to email a FreePBX call recording. the files are stored in /var/spool/asterisk/monitor/yyyy/mm/dd/ for example - /var/spool/asterisk/monitor/2014/07/10/conf-220-220... so when the recording is created, it checks to see who it goes to based on the filename, emails it and moves it out of the folder. I saw suggestions for inotify-tools, but the last news on the site is from 2010. I also saw incron but want something that has a small chance of messing up my phone system (I got scared of the installer) inotify.aiken.cz/?section=incron&page=doc&lang=en Because this version is early it does not contain a standard portable build mechanism (such as for autotools). There is only a Makefile which must be modified manually. On many Linux systems you need not to change anything.

Original source