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
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:19break
is desirable for my application. – MJiller Sep 10 '18 at 14:53break
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