Developer Guide: Automating HTTP Response Checks via Custom Scripting
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
- HTTP Status Code: Expecting a
200 OK, or perhaps a301/302for redirects. Anything in the5xxor4xxrange (except intentional auth challenges) should trigger a warning. - Response Time (Latency): A page loading in 30 seconds is functionally down. Set a strict timeout threshold (e.g., 5 seconds).
- 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.
- 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
- Open Task Scheduler from the Windows Start menu.
- Click Create Basic Task and name it
HTTP Response Monitor. - Set the trigger to Daily or When the computer starts, then configure it in the properties to repeat the task every 5 minutes.
- Set the action to Start a Program, point to
powershell.exe, and add-File C:\Scripts\Check-Website.ps1as the argument.
Common Mistakes and How to Fix Them
- Ignoring Redirects: If your script expects a
200but your site redirectshttp://tohttps://with a301, the script will fail unless you follow redirects automatically (e.g., usingcurl -Lorrequests.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.