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.