feat(docker-nuke): add run_timeout helper with pure-bash fallback

This commit is contained in:
David Ibia
2026-07-19 21:52:05 +01:00
parent 36caeafd03
commit 4e1e09559b
2 changed files with 70 additions and 0 deletions
+32
View File
@@ -55,6 +55,38 @@ OPT_DEEP=0
bin_exists() { command -v "$1" >/dev/null 2>&1; }
# Run a command with a hard time limit. Returns 124 if it timed out.
# Call inside a conditional so `set -e` does not abort on a non-zero return.
run_timeout() {
local secs="$1"
shift
if bin_exists timeout; then
timeout "$secs" "$@"
return $?
fi
if bin_exists gtimeout; then
gtimeout "$secs" "$@"
return $?
fi
# Pure-bash fallback: run in background and poll.
"$@" &
local pid=$!
local waited=0
while kill -0 "$pid" 2>/dev/null; do
if [ "$waited" -ge "$secs" ]; then
kill -9 "$pid" 2>/dev/null || true
wait "$pid" 2>/dev/null || true
return 124
fi
sleep 1
waited=$((waited + 1))
done
local rc=0
wait "$pid" || rc=$?
return "$rc"
}
log() { printf '%s\n' "$*"; }
warn() { printf '%s\n' "$*" >&2; }