I have a while
loop in a bash
script which should do something different at the beginning and at every 5 second interval. Any previous loop is allowed to complete. The 5s interval is indicated by the do_different
global variable set by the heartbeat
function. An additional complication is that a normal while
loop completes in an unknown amount of time (simplified with RANDOM
in below script).
Using cron
is not an option, neither is timing the random process.
I already unsuccessfully tried using a pipe as well as process substitution. The whole script may be re-factored.
#!/bin/bash
function heartbeat {
do_different=true
while sleep 5s
do
do_different=true
done
}
heartbeat &
while true
do
if $do_different
then
echo 'Something different'
do_different=false
i=0
else
# process of random duration; not important
r=$(( 1 + RANDOM % 3 ))
sleep "${r}s"
i=$((i + r))
echo "$i"
fi
done