Skip to content

Instantly share code, notes, and snippets.

@j796160836
Last active July 24, 2026 09:08
Show Gist options
  • Select an option

  • Save j796160836/fd54809788e4c4a2046b8e2839b16737 to your computer and use it in GitHub Desktop.

Select an option

Save j796160836/fd54809788e4c4a2046b8e2839b16737 to your computer and use it in GitHub Desktop.
retry10.sh
param(
[Parameter(Mandatory = $true, ValueFromRemainingArguments = $true)]
[string[]]$Command
)
for ($i = 1; $i -le 10; $i++) {
Write-Host "Trying $i..."
& $Command[0] $Command[1..($Command.Length - 1)]
if ($LASTEXITCODE -eq 0) {
break
}
Write-Host "Sleep 5 sec..."
Start-Sleep -Seconds 5
}

Retry 10

Retry 10 times. Sleep 5 seconds when retry.

Linux/Mac

./retry10.sh <your_command>

Windows (Powershell)

./retry10.ps1 <your_command>
# retry10.sh
# Retry command 10 times if fail
# Usage: ./retry10.sh <your_command>
for i in {1..10}; do
echo "Trying $i..."
"$@" && break
[ $i -eq 10 ] && echo "Command failed after 10 attempts" && exit 1
echo "Sleep 5 sec..."
sleep 5
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment