Designing event based applications with incron

Recently we wrote an article on leveraging AWS Lambda to create event based applications using S3. However what happens when you don’t have access to S3? What if you are using FTP or shared drives? Luckily there are still solutions! One way to accomplish this on Linux  is using incron.

This service allows you to setup actions and scripts to trigger based on file events. This eliminates the need to write scripts that constantly poll for files or having to integrate “file watching” code into your application. Setting up and running these applications is extremely simple below is a simple guide to get you up and running.

Installing incron

Installing incron can be done via your system package manager if you are using RPM or DEB package repositories. Otherwise you can build it from source by downloading the source code and following the installation instructions for your system. After you’ve installed incron you will want to modify the /etc/incron.allow file to include any users that you want to grant permission to edit the incrontab.

Setting up a simple test

Now that the system is installed we can experiment with a simple test to make sure it is working. To do this execute the following command

$ incrontab -e

Be sure to execute the command as one of the white-listed users in /etc/incron.allow if the file wasn’t created during installation that means all users have permission to edit the incrontab.

Once you are in the editor, add the following line

/home/test/watched-files IN_CREATE logger -t simple-test -p user.notice "Found file $#"

Now you just need to start incron by running service incrond start (Note on some systems “incrond” maybe “incron”). If you drop a file into the directory you should see a message in your syslog.

$ touch /home/test/watched-files/something.txt
$ tail /var/log/syslog | grep 'simple-test'
Jul 28 12:00:00 localhost.localdomain simple-test[3092]: Found file something.txt

Lets break down the above watcher that we added to the incrontab..

[directory to watch] [file action/mask] [command] [command options]

  • directory to watch is the directory to monitor for the file actions. NOTE: subdirectories are not included you will need to repeat your watcher line for every subdirectory you want to include.
  • file action/mask this is the file event that should trigger the notification. You can find a list of supported actions here.
  • command this is the script or command that should be executed when a file event occurs. If you are calling custom scripts please note the Gotchas section below.
  • command options these are the command arguments that should be forwarded.

Common Gotchas

Upon using incron I realized there are several things that separate its functionality from regular cron. You will want to know these ahead of time to save yourself a lot of headache thanks to the silent-fail nature of incron.

Wrap incrontab commands with shell script

Unlike CRON incrontab does not handle complex commands — such as redirection or capturing stdout — very well so its recommended you wrap any of your commands in a bash script.

Any script that is called with incrontab should have an extension

There is a bug where incron does not honor the executable permission so you need to do one of the following things to fix this…

  • Call /bin/sh before your script like /bin/sh /path/to/myscript
  • Append the proper script extension when saving your file like /path/to/myscript.bash
Use absolute paths

Shorthand or relative paths will silently break the system. This includes binaries and common executables like chown. If you are unsure where a command is located simply do which [command] and it will tell you where the command is located.

In conclusion

incron provides a unique way to handle your scripts and applications by removing the cumbersome process of polling files and triggering scripts with them. This allows you to shift your energy off of writing code to poll files and focus it on core logic and the problems you need to solve with the application. At Koddi we believe in the idea of not reinventing the wheel and incron allows us to do that by effortlessly integrating with our systems.

Categories
Technology