I am looking write a small bash script to, when launched, watch a directory for any newly created files. If a new file appears, I want its presence to trigger a second script to run.
I see this being used to trigger the compression recently digitized video, and add it to a log of ingested footage.
Currently my code looks like this:
#!/bin/sh
##VIDSTAT is a global variable coming from a parent script.
##proj is the ingestion directory coming from a parent script
proj=$1
dir="/home/$USER/data/movies/$proj"
dirlist=$(ls $dir)
while { $VIDSTAT -eq 1 }:
do
for mov in $dirlist
do
if [ "$(( $(date +"%s") - $(stat -c "%Y" $mov) ))" -lt "5" ]
then
~/bin/compressNlog.sh $mov
fi
done
done
Is there an easier/cleaner/less memory intensive way to do this?
EDIT I will be changing the ingestion directory per capture session. I have adjusted the code accordingly
How about incron? It triggering Commands On File/Directory Changes.
Example:
Where
<path>can be a directory (meaning the directory and/or the files directly in that directory (not files in subdirectories of that directory!) are watched) or a file.<mask>can be one of the following:<command>is the command that should be run when the event occurs. The following wildards may be used inside the command specification:If you watch a directory, then $@ holds the directory path and $# the file that triggered the event. If you watch a file, then $@ holds the complete path to the file and $# is empty.
Working Example:
Start Daemon:
Edit
incrontabfileTest it
Result:
Notes: In
Ubuntuyou need to activate inotify at boot time. Please add following line in Grub menu.lst file: