Skip to content

Instantly share code, notes, and snippets.

@meysam81
Created August 10, 2025 15:12
Show Gist options
  • Select an option

  • Save meysam81/4e5af66a53a5115451e697c99678f2df to your computer and use it in GitHub Desktop.

Select an option

Save meysam81/4e5af66a53a5115451e697c99678f2df to your computer and use it in GitHub Desktop.
#!/bin/bash
URL="${1:-https://api.example.com/endpoint}"
OUTPUT_FILE="${2:-}"
CURL_CMD="curl \
--retry 3 \
--retry-delay 2 \
--retry-max-time 30 \
--retry-all-errors \
--max-time 10 \
--connect-timeout 5 \
--location \
--fail \
--silent \
--show-error"
if [ -n "$OUTPUT_FILE" ]; then
CURL_CMD="$CURL_CMD --output \"$OUTPUT_FILE\""
fi
CURL_CMD="$CURL_CMD \"$URL\""
echo "Fetching: $URL"
if eval $CURL_CMD; then
echo "Success"
exit 0
else
EXIT_CODE=$?
echo "Failed after retries. Exit code: $EXIT_CODE" >&2
exit $EXIT_CODE
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment