Skip to content

Instantly share code, notes, and snippets.

@mttjohnson
Created November 20, 2025 06:41
Show Gist options
  • Select an option

  • Save mttjohnson/04630b4fab661be5c976c2ccd6d88e4d to your computer and use it in GitHub Desktop.

Select an option

Save mttjohnson/04630b4fab661be5c976c2ccd6d88e4d to your computer and use it in GitHub Desktop.
http continuous ping script for monitoring an http service
#!/bin/bash
# Simple HTTP Monitor Script
URL="$1"
INTERVAL="${2:-5}"
if [ -z "$URL" ]; then
echo "Usage: $0 <URL> [interval_seconds]"
echo "Example: $0 https://google.com 2"
exit 1
fi
echo "Monitoring $URL every ${INTERVAL} seconds (Ctrl+C to stop)"
echo "TIMESTAMP | CODE | CONN | START | TOTAL | SIZE | URL"
echo "----- ------ -----|------|-------|-------|-------|-------|-------------"
while true; do
# Get current timestamp
timestamp=$(date +"%Y-%m-%d %H:%M:%S")
# Make curl request and capture timing info
result=$(curl -s -w "%{http_code}\t%{time_connect}\t%{time_starttransfer}\t%{time_total}\t%{size_download}\n" \
-o /dev/null \
--max-time 10 \
--connect-timeout 5 \
"$URL")
# Parse the tab-separated values
status_code=$(echo "$result" | cut -f1)
connect_time=$(echo "$result" | cut -f2)
starttransfer_time=$(echo "$result" | cut -f3)
total_time=$(echo "$result" | cut -f4)
download_size=$(echo "$result" | cut -f5)
# Function to convert to milliseconds (truncate to whole numbers)
convert_to_ms() {
local time_val="$1"
if [ -z "$time_val" ] || [ "$time_val" = "0.000000" ]; then
echo "0"
return
fi
# Convert to integer (truncate decimal part)
echo "$time_val" | awk '{print int($1 * 1000)}' 2>/dev/null || echo "0"
}
connect_ms=$(convert_to_ms "$connect_time")
starttransfer_ms=$(convert_to_ms "$starttransfer_time")
total_ms=$(convert_to_ms "$total_time")
# Handle curl errors
if [ "$status_code" = "000" ]; then
connect_ms="0"
starttransfer_ms="0"
total_ms="0"
status_code="ERROR"
fi
# Format output
printf "%-15s | %-4s | %-5s | %-5s | %-5s | %-5s | %s\n" \
"$timestamp" \
"$status_code" \
"${connect_ms}ms" \
"${starttransfer_ms}ms" \
"${total_ms}ms" \
"$download_size" \
"$URL"
sleep "$INTERVAL"
done
@mttjohnson
Copy link
Author

This was based on some previous work doing similar things at this gist https://gist.github.com/mttjohnson/a93c6a3c3b11778873c24b5dfac793b8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment