How to trigger a cron job based on file modifications

Cron is an amazon tool that allowing Linux and Unix users to run commands or scripts at a given date and time. However, sometimes people want to trigger a job based on file modifications / creations. Here we can use incron to do this kinds of automation.

incron

Incron installation

1
sudo apt-get install incron

Using Incron

If /etc/incron.allow exists only users listed here may use incron. Otherwise if /etc/incron.deny exists only users NOT listed here may use incron. If none of these files exists everyone is allowed to use incron.

So firstly, we need to modify incron.allow to add users in:

1
nano incron.allow

Add users e.g. root, yuxi into the list:

1
2
root
yuxi

Now we can list all incron jobs:

1
incrontab -l

Set up incron job for user ‘yuxi’

1
incrontab -e

Add the following line to do an auto deployment to git repository:

1
/home/yuxi/website_src/ IN_MODIFY /home/yuxi/deploy_website.sh
Share