Skip to main content
removed the comment on 'break', to match the requirement
Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 264

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
        ##breakbreak           ## 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.

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.

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.

Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 264

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.