Color-Coded URL Status Checker in Bash: Enhanced Script for Fast Web Health Monitoring

2025-10-17 3 min read Bash Scripting Devops Monitoring

Introduction: check-urls.sh – Fast, Visual URL Status Checks in Bash

Monitoring the health of multiple URLs is a common DevOps and web operations task. But staring at a wall of status codes can be tedious. What if you could instantly spot issues with color-coded output, all from a simple Bash script?

This post walks you through an improved Bash script that:

  • Reads URLs from a file
  • Checks their HTTP status using curl with a configurable timeout
  • Outputs results as status_code url, color-coded for instant readability

Body: Building a Smarter, More Readable URL Checker

check-urls.sh: The Enhanced Script

Below is the script. It’s concise, robust, and easy to integrate into any workflow.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/bash

# check-urls.sh: Color-coded HTTP status checker for URLs in a file

set -o nounset

URL_FILE="${1:-/tmp/urls2}"
TIMEOUT="${2:-5}"   # Default timeout in seconds

if ! command -v curl >/dev/null 2>&1; then
    echo "Error: curl is not installed." >&2
    exit 1
fi

if [[ ! -f "$URL_FILE" ]]; then
    echo "Error: File '$URL_FILE' not found." >&2
    exit 1
fi

# Color codes for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
GRAY='\033[1;30m'
NC='\033[0m' # Reset

while IFS= read -r line; do
    # Skip empty lines and comments
    [[ -z "$line" || "$line" =~ ^# ]] && continue

    # Fetch HTTP status code with timeout
    code=$(curl --max-time "$TIMEOUT" -s -o /dev/null -w "%{http_code}" "$line")

    # Choose color by status code
    if [[ "$code" =~ ^2[0-9][0-9]$ ]]; then
        color="$GREEN"
    elif [[ "$code" =~ ^3[0-9][0-9]$ ]]; then
        color="$YELLOW"
    elif [[ "$code" =~ ^4[0-9][0-9]$ || "$code" =~ ^5[0-9][0-9]$ ]]; then
        color="$RED"
    else
        color="$GRAY"
    fi

    # Output: status_code url
    echo -e "${color}${code} ${line}${NC}"
done < "$URL_FILE"

check-urls.sh: Usage and Customization

  • Input file: By default, the script reads from /tmp/urls2. You can specify another file as the first argument.
  • Timeout: The second argument sets the curl timeout (default: 5 seconds).
  • Output: Each line shows the HTTP status code (colored) followed by the URL.

Example

Suppose urls.txt contains:

1
2
3
4
5
https://google.com
https://httpstat.us/404
https://httpstat.us/500
# This is a comment
https://httpstat.us/301

Run:

1
./check-urls.sh urls.txt 3

You’ll see:

  • Green for 2xx (OK)
  • Yellow for 3xx (redirects)
  • Red for 4xx/5xx (errors)
  • Gray for anything unexpected or unreachable

check-urls.sh: Why Color Matters

Color-coding status codes makes it trivial to spot issues in large lists:

  • Green (2xx): All good
  • Yellow (3xx): Redirects, worth checking
  • Red (4xx/5xx): Broken or server errors, needs attention
  • Gray: Timeouts, malformed responses, or unknown codes

This approach is especially useful in CI/CD pipelines, monitoring scripts, or manual audits.

Conclusion: Takeaways and Next Steps

A color-coded URL status checker in Bash is a simple yet powerful tool for web monitoring and troubleshooting. By leveraging curl with timeouts and ANSI color codes, you get fast, readable feedback on the health of your endpoints.

comments powered by Disqus