Bash Timeout Loops: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
m (Ovidiu moved page Bash Timeout Loop to Bash Timeout Loops without leaving a redirect)
Line 3: Line 3:


=Overview=
=Overview=
Pure timeout.


<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
Line 13: Line 15:
     sleep 1
     sleep 1
     remaining=$(expr ${timeout_sec} + ${t0} - $(date '+%s'))
     remaining=$(expr ${timeout_sec} + ${t0} - $(date '+%s'))
done
</syntaxhighlight>
Loop until a command succeeds or a timeout occurs.
<syntaxhighlight lang='bash'>
x=1
test-command
while [ ! $? -eq 0 ]
do
    sleep 10
    x=$(( $x + 1 ))
    if [ $x -gt 100 ]
    then
      exit 255
    fi
    test-command
done
done
</syntaxhighlight>
</syntaxhighlight>

Revision as of 19:51, 5 May 2021

Internal

Overview

Pure timeout.

...
local timeout_sec=...
local t0=$(date '+%s')
local remaining=$(expr ${timeout_sec} + ${t0} - $(date '+%s'))
while [[ ${remaining} -gt 0 ]]; do
    debug "remaining ${remaining} secs ..."
    sleep 1
    remaining=$(expr ${timeout_sec} + ${t0} - $(date '+%s'))
done

Loop until a command succeeds or a timeout occurs.

x=1
test-command
while [ ! $? -eq 0 ]
do
    sleep 10
    x=$(( $x + 1 ))

    if [ $x -gt 100 ]
    then
       exit 255
    fi

    test-command
done