I'm trying to adapt the script below (taken from https://superuser.com/questions/181517/how-to-execute-a-command-whenever-a-file-changes) to cause my system to record a video stream whenever a particular file changes on my system:
#!/bin/bash
# Set initial time of file
LTIME=`stat -c %Z /path/to/monitored/file`
while true    
do
   ATIME=`stat -c %Z /path/to/monitored/file`
   if [[ "$ATIME" != "$LTIME" ]]
   then    
       echo "RUN COMMAND"
       LTIME=$ATIME
   fi
   sleep 5
done
As is evident, the script polls the time stamp of the monitored file every 5 seconds to see whether it has changed and, if it has, echoes something to the terminal.
Here's what I've got so far that almost works as I want it to for my purposes (recordingscript.sh uses mplayer -dumpstream -dumpfile mystream URL to record a stream for 70 minutes):
#!/bin/bash
# Set initial time of file
LTIME=`stat -c %Z /path/to/monitored/file`
while true    
do
   ATIME=`stat -c %Z /path/to/monitored/file`
   if [[ "$ATIME" != "$LTIME" ]]; then
      /path/to/my/recordingscript,sh
        break
      LTIME=$ATIME
   fi
   sleep 5
done
The problem that remains is that I would like for this script to run as a cron job each day within, say, a 5 hour time frame. If no change is made to the monitored file within that time frame, I'd simply like for the script to abort/exit until the next time cron starts it. How can I modify my adapted script so that it will only run for about 5 hours, then exit? I realize I could probably do this by invoking the script with timeout 300m but I thought there might be other, possibly better, solutions for doing this