Skip to content

URL Status Check

URL Status Check #2

Workflow file for this run

name: URL Status Check
on:
schedule:
- cron: "*/15 * * * *" # Run every 15 minutes
jobs:
check_urls:
runs-on: ubuntu-latest
steps:
- name: URL Status Check
run: |
# Define an array of URLs to check
urls=("https://example.com/api/v1/resource1" "https://example.com/api/v1/resource2" "https://example.com/api/v1/internal-resource")
# Prepare an empty results array to hold URL and status code
results=()
# Loop through each URL and make a GET request using curl
for url in "${urls[@]}"; do
# Make the GET request and capture the HTTP status code and any potential errors
status_code=$(curl -o /dev/null -s -w "%{http_code}" --connect-timeout 5 --max-time 10 "$url")
# Check if the curl command was successful
if [ $? -ne 0 ]; then
# If curl fails (e.g., connection timeout), set status code to 503 (unreachable)
if [ "$status_code" -eq "000" ]; then
echo "URL: $url is unreachable (timeout or connection failure). Setting response code to 503."
status_code=503
else
# If another error occurs (e.g., DNS resolution failure), set status code to 0
echo "URL: $url encountered an error. Setting response code to 0."
status_code=0
fi
fi
# Append the result with the actual or adjusted status code
echo "URL: $url, Status Code: $status_code"
results+=("{\"url\":\"$url\",\"response_code\":$status_code}")
done
# Join the results array into a JSON array string
results_json=$(printf "%s\n" "${results[@]}" | jq -s '.')
# Print the JSON to see the results
echo "Results: $results_json"
# Send the results to the specified endpoint using curl
post_results_url="https://your-api-endpoint.com/submit"
curl -X POST "$post_results_url" \
-H "Content-Type: application/json" \
-d "$results_json"