-1

Appendix: the code below runs fine, as Matthias pointed out. The err happened at another place. In short: if you want that sleep is changed during script runtime, e.g. due to a certain event, you might use the code below.

Original description:

My bash script ought to check a certain status - e.g. the existence of a file - every 5 minutes. If the status is as expected, everything is fine. But if the status is otherwise, the checks ought to happen in a shorter frequency, until everything is normal again.

Example:

NORMAL_SLEEP=300
SHORT_SLEEP=30
CUR_SLEEP=''

while :
do

if [ -f /tmp/myfile ]; then
    logger "myfile still exists. Next check in 5min"
    CUR_SLEEP=$NORMAL_SLEEP
else
     logger "myfile disappeared. Check again in 30s!"
     CUR_SLEEP=$SHORT_SLEEP
     echo "/tmp/myfile was removed. Check this!" \
     | mailx -s "alert: myfile missed" johndoe@somewhere.com
fi

trap 'kill $SLEEP_PID; exit 1' 15
sleep $CUR_SLEEP &
SLEEP_PID=$!
wait

done

Problem: the sleep time does not adapt...

Had a look at Bash Script: While-Loop Subshell Dilemma but unfortunately can't see how it could solve my problem.

MarkHelms
  • 55
  • 10
  • Possible duplicate of [Bash Script: While-Loop Subshell Dilemma](http://stackoverflow.com/questions/13726764/bash-script-while-loop-subshell-dilemma) – Aaron Mar 01 '17 at 13:32
  • 3
    Which sleep length is it stuck on, if it does not adapt? Also, do the messages change - which ones come out? – Mark Setchell Mar 01 '17 at 13:34
  • @MarkSetchell sleep stays at default 300s/5min; messages are correct because the missing file is recognised, but the intended change of sleep does not happen. Checks keep running every 5mins. – MarkHelms Mar 01 '17 at 13:46
  • 2
    @Aaron `CUR_SLEEP` isn't set in any subshells here. – chepner Mar 01 '17 at 13:55
  • @chepner agreed, but the post presented the code as an example, so I thought it might be worth a shot. I should just have linked the question rather than tagged as duplicate though. – Aaron Mar 01 '17 at 13:59
  • @Aaron is it possible to remove the duplicate tag? Thanks for your support, anyway! – MarkHelms Mar 01 '17 at 15:29
  • @MarkHelms sure, removed ; technically the question should be closed as the error couldn't be reproduced with the posted code though, the reasoning being that it won't help people checking it. – Aaron Mar 01 '17 at 18:49

1 Answers1

1

The code ran fine on my machine. Here's what I ran (changed the time values just to test):

  • ./script.sh --> "myfile disappeared. Check again in 30s!" printed at 2 sec intervals
  • touch /tmp/myfile
  • ./script.sh --> "myfile still exists. Next check in 5min" printed at 5 sec intervals

The file, script.sh:

#!/bin/bash

NORMAL_SLEEP=5
SHORT_SLEEP=2
CUR_SLEEP=''

while :
do

if [ -f /tmp/myfile ]; then
    echo "myfile still exists. Next check in 5min"
    CUR_SLEEP=$NORMAL_SLEEP
else
     echo "myfile disappeared. Check again in 30s!"
     CUR_SLEEP=$SHORT_SLEEP
     echo "/tmp/myfile was removed. Check this!" \
     | mailx -s "alert: myfile missed" johndoe@somewhere.com
fi

trap 'kill $SLEEP_PID; exit 1' 15
sleep $CUR_SLEEP &
SLEEP_PID=$!
wait

done

And I probably sent some mail to johndoe@somewhere.com but that's okay.

Matthias
  • 3,160
  • 2
  • 24
  • 38
  • Yes, you are right! I had a glitch further down my script, where I evaluated a variable falsely and thus reset the sleep to default (not to see in the example above). Sorry. – MarkHelms Mar 01 '17 at 15:24