XiaTools

Developer Guide: Automating HTTP Response Checks via Custom Scripting

Updated 10 Oct 2026

Automating an HTTP response check script is the most reliable way to monitor website availability, track unexpected status code changes, and catch downtime before your users do. Whether you are managing a cluster of microservices or a simple corporate portal, having a script run in the background saves hours of manual troubleshooting. If you need an instant, no-code check right now, you can use the Website Down Checker to quickly verify whether a URL is globally accessible and returning the expected status. For continuous monitoring and deep automation, however, you need custom scripts integrated into your CI/CD pipeline, cron jobs, or serverless functions.

Why Automate HTTP Response Checks?

Manual testing does not scale. When a server crashes or an SSL certificate expires at 3 AM, you need automated scripts running every minute to log the event and trigger alerts.

Benefits of Automated HTTP Monitoring

  • Proactive Alerting: Catch broken deployments and server timeouts immediately.
  • Historical Logging: Track response times over days and weeks to spot performance degradation.
  • Automated Failover: Trigger routing adjustments or server restarts when endpoints fail.
  • Compliance & SLA Tracking: Prove uptime metrics to stakeholders with hard log data.

Designing Your Response Check Script

A robust HTTP response check script should do more than just send a basic GET request. It needs to evaluate multiple criteria to ensure the application is truly healthy.

Key Metrics to Evaluate

  1. HTTP Status Code: Expecting a 200 OK, or perhaps a 301/302 for redirects. Anything in the 5xx or 4xx range (except intentional auth challenges) should trigger a warning.
  2. Response Time (Latency): A page loading in 30 seconds is functionally down. Set a strict timeout threshold (e.g., 5 seconds).
  3. Content Verification: Checking that the body contains a specific string or JSON key confirms the application layer and database are responding, not just the web server process.
  4. SSL/TLS Validity: Ensuring the certificate is valid and not expiring soon.

Writing the Automation Scripts

Here are production-ready scripts in Bash, Python, and PowerShell. Each checks https://example.com (using documentation domains) and validates the response.

Bash and cURL Script

Bash scripts are ideal for lightweight cron jobs running on Linux or macOS servers.

#!/bin/bash

URL="https://example.com"
TIMEOUT=10
EXPECTED_CODE=200

# Perform the request and get the HTTP status code
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" --max-time $TIMEOUT "$URL")

if [ "$RESPONSE" -eq "$EXPECTED_CODE" ]; then
    echo "[SUCCESS] $URL returned $RESPONSE"
    exit 0
else
    echo "[ALERT] $URL returned status $RESPONSE (Expected: $EXPECTED_CODE)"
    # Insert webhook notification command here (e.g., Slack or Discord)
    exit 1
fi

Python Script (Requests Library)

Python offers robust error handling and easy integration with logging frameworks and notification APIs.

import sys
import requests

URL = "https://example.com"
TIMEOUT = 5
EXPECTED_CODE = 200

try:
    response = requests.get(URL, timeout=TIMEOUT)
    if response.status_code == EXPECTED_CODE:
        print(f"[SUCCESS] {URL} returned {response.status_code} in {response.elapsed.total_seconds()}s")
        sys.exit(0)
    else:
        print(f"[ALERT] {URL} returned status {response.status_code}")
        sys.exit(1)
except requests.exceptions.Timeout:
    print(f"[CRITICAL] {URL} timed out after {TIMEOUT} seconds")
    sys.exit(2)
except requests.exceptions.RequestException as e:
    print(f"[CRITICAL] Connection error for {URL}: {e}")
    sys.exit(2)

PowerShell Script

PowerShell is perfect for Windows Server environments and Azure automation runbooks.

$Url = "https://example.com"
$TimeoutSec = 10

try {
    $Request = [System.Net.WebRequest]::Create($Url)
    $Request.Timeout = $TimeoutSec * 1000
    $Response = $Request.GetResponse()
    $StatusCode = [int]$Response.StatusCode
    
    if ($StatusCode -eq 200) {
        Write-Host "[SUCCESS] $Url returned status $StatusCode" -ForegroundColor Green
        exit 0
    } else {
        Write-Host "[ALERT] $Url returned status $StatusCode" -ForegroundColor Yellow
        exit 1
    }
}
catch {
    Write-Host "[CRITICAL] Failed to reach $Url. Error: $_" -ForegroundColor Red
    exit 2
}

Comparing Automation Approaches

Approach Setup Complexity Best Use Case Infrastructure Cost Scalability
Cron + Bash Low Single server health checks Free (Runs on existing server) Low
Python + Cron Medium Custom logic, logging, and webhooks Free (Runs on existing server) Medium
Cloud Functions High Distributed multi-region monitoring Pay-per-execution High

Scheduling and Execution

Once your script is ready, you need to automate its execution frequency using task schedulers.

Linux Cron Job

To run your Bash or Python script every 5 minutes, open your crontab editor (crontab -e) and add the following line:

*/5 * * * * /usr/bin/python3 /path/to/check_script.py >> /var/log/http_check.log 2>&1

Windows Task Scheduler

  1. Open Task Scheduler from the Windows Start menu.
  2. Click Create Basic Task and name it HTTP Response Monitor.
  3. Set the trigger to Daily or When the computer starts, then configure it in the properties to repeat the task every 5 minutes.
  4. Set the action to Start a Program, point to powershell.exe, and add -File C:\Scripts\Check-Website.ps1 as the argument.

Common Mistakes and How to Fix Them

  • Ignoring Redirects: If your script expects a 200 but your site redirects http:// to https:// with a 301, the script will fail unless you follow redirects automatically (e.g., using curl -L or requests.get(..., allow_redirects=True)).
  • Not Setting Timeouts: Without a strict timeout rule, your script can hang indefinitely if the remote server drops packets, consuming thread pools and locking up your scheduler.
  • False Positives from Transient Network Blips: Wrap your alert logic in a retry loop. Require a check to fail two or three consecutive times before sending an alert to avoid paging you at night over a momentary network hiccup.
  • Hardcoding Secrets: If your script needs to check endpoints protected by API tokens or basic auth, never hardcode credentials. Use environment variables or secure secret managers.

Automation Deployment Checklist

  • Script successfully validates status codes and throws non-zero exit codes on failure.
  • Network timeout limits are explicitly defined (e.g., 5 to 10 seconds).
  • Redirect behavior (301/302) is handled correctly.
  • Task scheduler or cron job is configured with appropriate execution intervals.
  • Output logs are directed to a file or log management system.
  • Alert mechanisms (webhooks, email, SMS) are tested and verified.

Frequently asked questions

How often should I run an automated HTTP response check script?

For most web applications, running checks every 1 to 5 minutes provides a good balance between timely alert generation and avoiding unnecessary load on your target server. High-availability enterprise applications may require checks every 30 seconds.

How can I make my script send alerts to Slack or Discord?

You can modify your script's error handling block to execute an HTTP POST request to an incoming webhook URL provided by Slack or Discord. Include the error details, timestamp, and failing URL in the JSON payload of the webhook.

What should my script do if the target server returns a 301 or 302 redirect?

Your script should either be configured to automatically follow redirects and evaluate the final destination status code, or you should explicitly treat 301 and 302 as expected codes if you are testing a redirect rule.

Can I check internal endpoints using these scripts?

Yes, as long as the machine executing the script has network access to the internal network, VPC, or private IP range (such as 192.0.2.0/24). Ensure firewalls and security groups permit outbound requests from the monitoring script host.

Why is my script reporting a timeout even though the website loads in my browser?

Browsers often cache assets, handle TLS handshakes differently, or may be bypassing restrictive corporate firewalls and proxies that your script server encounters. Check your script's timeout threshold and verify that network routes and DNS resolution work correctly from the script host.

Related articles

Free tools