2

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 the 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. It will also be clear that the echo command is just a sort of proof-of-concept and that just about any other command could take its place in the script.

Here's what I've got so far that almost works as desired 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 weekday 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

  • Is it your intention to run the recording script only once within that window? That's what the break is doing now, but it's conceivable that the timestamp changes shortly after the script begins, then the recording runs for 70 minutes, giving enough time for the timestamp to change again within the 5-hour window. Should the script exit after one recording, or should it run for the full 5 hours? – Jeff Schaller Sep 6 '18 at 20:19
  • Thanks for offering those improvements. For my use scenario the script should run only once within the 5-hour window. I'm looking to record either a 7PM showing or an 11PM showing, so once the recording has been triggered the script should exit. So the break is desirable for my application. – MJiller Sep 10 '18 at 14:53
  • To be more precise, "For my use scenario the script should run only once within the 5-hour window" should actually read "For my use scenario the script should trigger the recording only once within the 5-hour window." – MJiller Sep 10 '18 at 15:01
  • Then I'd suggest uncommenting the break statement in my script, below. Try it out and let me know (or just vote/accept it) if it works for you. If not, please let me know that as well! – Jeff Schaller Sep 10 '18 at 15:06
1

I would suggest the following script, in which I've made a few changes:

  • used lower-case variable names
  • assigned a "give up" variable with a spelled-out expression of "5 hours in seconds"
  • parameterized the file to monitor in the filetomonitor variable
  • changed the while true loop to be one that loops for 5 hours, based on the behavior of the SECONDS bash variable, which counts the number of seconds since the shell was started. This starts at zero when the shell script stars.
  • changed the backticks to the newer $( ... ) form

The updated script:

#!/bin/bash
giveup=$((5 * 60 * 60))
filetomonitor='/tmp/file-to-monitor'

ltime=$(stat -c %Z "$filetomonitor")
while [[ "$SECONDS" -lt "$giveup" ]]
do
  atime=$(stat -c %Z "$filetomonitor")
  if [[ "$atime" -ne "$ltime" ]]
  then
        echo Take action
        break           ## if we're only supposed to act once
        ltime=$atime
  fi
  sleep 5
done

I've left the break in there, but commented out. If you want the script to exit (before the 5 hours) after executing the action, then uncomment the break line; otherwise, the current version of the script will execute for 5 hours (possibly longer, if the 70-minute action starts at 4:59), possibly calling the action multiple times.

  • Nice. Preliminary testing seems to indicate that this script works. Less kludgy than starting the script with timeout. I'll test a bit more and if I continue to meet with success, I'll mark this response as the solution. – MJiller Sep 10 '18 at 20:48
  • 1
    Continuing testing this solution I'm discovering that it is frequently failing since, for some strange reason, the recording script it triggers fails to start the recording (throws error messge). Yet if I try restarting the stream recording script, it will succeed the second time. I think I need to test now with the break line commented out since that way I should be able to retry triggering the stream-recording script. Will report back if that resolves the issue. – MJiller Sep 22 '18 at 0:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.