Skip to content

Instantly share code, notes, and snippets.

@EthraZa
Created August 25, 2016 20:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EthraZa/b21415e904165b19752ed9baf1a805a1 to your computer and use it in GitHub Desktop.
Save EthraZa/b21415e904165b19752ed9baf1a805a1 to your computer and use it in GitHub Desktop.
Script for incron to add or remove entries from an incrontab file
#!/bin/bash
###
# sync2incron - Script for incron to add or remove entries from an incrontab file
#
# Usage:
#
# - Add it to incron as something like:
# /root/path/to/monitor IN_CREATE,IN_DELETE /path/to/sync2incron.sh $% /etc/incron.d/myincrontab $@/$# IN_CLOSE_WRITE myscript $$@/$$#
#
# - Little aid in creating the incrontab file with directories that have 14 numbers only in name. You may remove or change the regex to fit your needs:
# find /root/path/to/monitor -type d -regextype sed -regex ".*/[0-9]\{14\}/" -print0 | xargs -0 -I{} echo "{} IN_CLOSE_WRITE myscript \$@/\$#" > /etc/incron.d/myincrontab
#
#
# Changelog:
#
# 1.0 - 2016/08/25 - allan (at) Spotapp .mobi
# - First version
#
# --
# By Allan Brazute - EthraZa (at) gmail .com
###
if [ $# -gt 4 ]; then
if [ -f "$2" ]; then
ln="${@:3}"
if [[ $1 == *"CREATE"* ]]; then
if [ `grep -c "$ln" "$2"` -gt 0 ]; then
echo "$3 is already on incrontab"
else
echo "${@:3}" >> $2
echo "$3 added to $2"
fi
elif [[ $1 == *"DELETE"* ]]; then
if [ `grep -c "$ln" "$2"` -gt 0 ]; then
cp -f "$2" "/tmp/`basename $2`.bak"
grep -hsv "$ln" "/tmp/`basename $2`.bak" > "$2"
echo "$3 deleted from $2"
else
echo "$3 not found on $2"
fi
fi
else
echo "Incrontab $2 not found"
fi
else
echo "We need some parameters to work with:"
echo "What to do (CREATE or DELETE); Path to incrontab file; Incron line to append or remove from incrontab;"
echo "Usage: sync2incron.sh CREATE /etc/incron.d/myincrontab /var/www/clients IN_CLOSE_WRITE myscript \\\$@/\\\$#"
fi
@EthraZa
Copy link
Author

EthraZa commented Aug 25, 2016

This is a generic bash script to monitor a directory with incron and add or remove new entries in this directory to an incrontab file to monitor too.
It is write in a simple manner so any one can use it and edit it to fit less generic needs.
If used with creativity it may give you a recursive incron. ;)

@charemer
Copy link

Thank you worked a treat - and no Python in sight 🥇 Using it to transcode security video files after creation. Very useful feature is the removal of watch directories from the table as in my case a new folder is created on with the first video of the day and folders are retained for period before being cleared and removed automatically. The videos created are uncompressed so being able to transcode asap after creation is very useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment