I haven’t done a post in a while so thought it was about time I finished off one of my 22 draft posts.
In this post I would like to mention incron. Simply put incron monitors the file system for events and when they occur it can execute commands that are defined in user or system tables in a similar way to cron.
So how is this useful you might ask, well in my case I wanted a way to monitor a directory of uploaded images on one server and when new ones are added copy them across to another server. But you can do absolutely anything you want with it, like monitor a directory for changes and alert by email of them.
Under new versions of Debian and Ubuntu you can install it using apt.
sudo apt-get install incron
If your distribution does not support install via package management you can download the source code and compile it yourself from the projects download page.
Start by editing the ‘/etc/incron.allow’
sudo vim incron.allow
in this case we are going to allow the user root to use incron. You can allow other users such as your own if you wish using line separated entries.
to the file then save and quit.
Really Simple Example
Next we will edit the incrontab itself
sudo incrontab -e
for a simple test add the following
/home/YOURUSERNAME IN_CLOSE_WRITE touch /tmp/incrontest-$#
save and exit.
What this does is when a file is written to your home directory the command `touch /tmp/incrontest-THE FILE NAME YOUR WROTE` is run.
To test it works follow these steps.
cd /home/YOURUSERNAME/ touch alpha touch delta cd /tmp/ ls -la incrontest-*
You should see output like
-rw-r--r-- 1 root root 0 2010-09-23 22:54 incrontest-alpha -rw-r--r-- 1 root root 0 2010-09-23 22:54 incrontest-delta
If your not sure if your EVENT is working as you expect you can take advantage of the Wildcards incron offers.
For example if you add
/tmp IN_CLOSE_WRITE echo "$$ $@ $# $% $&"
to your incrontab then go to the /tmp directory and create a new file you should get output like this when you check your ‘/var/log/syslog’ file.
Sep 23 23:13:57 localhost incrond: (root) CMD (echo "$ /tmp alpha IN_CLOSE_WRITE 8")
Just a few warnings of things that caught me out when I started using incron.
- You Can’t Monitor a Directory More than Once
If you try and monitor a directory more than once you will get an error similar to
Sep 23 23:24:02 localhost incrond: cannot create watch for user root: (16) Device or resource busy
if you want to monitor a dir for different actions use commas to separate the events e.g. ‘IN_CLOSE_WRITE,IN_DELETE’. Then if needed use the $% wildcard to pass the type of event that triggered the command to your script.
- Not Recursive
Currently only the parent directory is watched and no changes in the child directories will trigger an event. This may change in future versions.
Hopefully in this post I have given you a very quick idea of what incron is capable of. I could give a load more example of what to do with incron but to be honest its very simple and there are lots of good examples in the man documents so best to check them out. Also see the other links below in the resources section for more details.As usual if you have any questions please do send me an email or leave a comment.
- man 5 incrontab – for a list of event symbols and some good examples.
- incron documentation
- Scheduling jobs based on filesystem activity with incron
- Triggering Commands On File/Directory Changes With Incron