Bash Timeout Loops: Difference between revisions
Jump to navigation
Jump to search
(One intermediate revision by the same user not shown) | |||
Line 4: | Line 4: | ||
=Overview= | =Overview= | ||
Pure timeout | Pure timeout: | ||
<syntaxhighlight lang='bash'> | <syntaxhighlight lang='bash'> | ||
Line 10: | Line 10: | ||
local timeout_sec=... | local timeout_sec=... | ||
local t0=$(date '+%s') | local t0=$(date '+%s') | ||
local remaining=$(expr ${timeout_sec} + ${t0} - $(date '+%s')) | local remaining=$(expr ${timeout_sec} + ${t0} - "$(date '+%s')") | ||
while [[ ${remaining} -gt 0 ]]; do | while [[ ${remaining} -gt 0 ]]; do | ||
debug "remaining ${remaining} secs ..." | debug "remaining ${remaining} secs ..." | ||
sleep 1 | sleep 1 | ||
remaining=$(expr ${timeout_sec} + ${t0} - $(date '+%s')) | remaining=$(expr ${timeout_sec} + ${t0} - "$(date '+%s')") | ||
done | done | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Same thing, simpler: | |||
<syntaxhighlight lang='bash'> | |||
local t0 | |||
t0=$(date '+%s') | |||
local timeout_secs=10 | |||
while [[ $(expr ${timeout_secs} + ${t0} - "$(date '+%s')") -gt 0 ]]; do | |||
echo "." | |||
sleep 2 | |||
done | |||
</syntaxhighlight> | |||
Loop until a command succeeds or a timeout occurs. | Loop until a command succeeds or a timeout occurs. |
Latest revision as of 20:06, 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
Same thing, simpler:
local t0
t0=$(date '+%s')
local timeout_secs=10
while [[ $(expr ${timeout_secs} + ${t0} - "$(date '+%s')") -gt 0 ]]; do
echo "."
sleep 2
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