Skip to content

How to Monitor Web Service Availability in Batch Script

Learn how to create a persistent batch script using curl to continuously monitor web service uptime, log HTTP status codes, detect latency issues, and trigger alerts for failures or errors.

Maintaining "High Availability" for web services is a requirement for any modern IT environment. Whether you are managing an internal dashboard, an API gateway, or a customer-facing website, you need to know the moment the service goes down. A "Self-Healing" or "Monitoring" script doesn't just check the site once; it runs in a continuous loop, logging the uptime and alerting the administrator if the service stops responding or returns a server error (500).

This guide will explain how to build a persistent web monitoring script using curl.

Method: The Continuous Health Check Loop

This script will check a URL every minute and log the results to a CSV file for long-term auditing.

@echo off
setlocal enabledelayedexpansion

set "URL=https://api.mycompany.com/health"
set "LogFile=service_uptime.csv"
set "CheckInterval=60"

:: 1. Initialize Log File with Headers
if not exist "%LogFile%" >"%LogFile%" echo Date,Time,Status,Code

echo [MONITOR] Watching %URL%...
echo [MONITOR] Logs saved to %LogFile%. Press Ctrl+C to stop.

:CheckLoop
:: Default to 000 in case curl is not installed or produces no output
set "code=000"

:: 2. Use CURL to get the HTTP Status Code
for /f %%a in ('curl -s -o nul -w "%%{http_code}" --connect-timeout 10 "%URL%" 2^>nul') do set "code=%%a"

:: 3. Determine Status based on the HTTP code range
if "!code!"=="000" (
    set "status=OFFLINE"
) else if "!code:~0,1!"=="2" (
    set "status=ONLINE"
) else if "!code:~0,1!"=="3" (
    set "status=REDIRECT"
) else if "!code:~0,1!"=="4" (
    set "status=CLIENT_ERROR"
) else if "!code:~0,1!"=="5" (
    set "status=SERVER_ERROR"
) else (
    set "status=UNKNOWN"
)

:: 4. Log to console and file
echo [%time%] !status! - HTTP !code!
>>"%LogFile%" echo %date%,%time%,!status!,!code!

:: 5. Alert if status is not ONLINE
if not "!status!"=="ONLINE" (
    echo [ALERT] Service is not healthy! Status: !status!
)

timeout /t %CheckInterval% /nobreak >nul
goto :CheckLoop

Method 2: Detecting "Slow" Responses (Latency)

Sometimes a service is "Up" but it's so slow it's effectively "Down" for users. You can monitor the response time (latency).

@echo off
setlocal

:: Default to 0 in case curl is not installed or produces no output
set "latency=0"

:: Capture 'time_total' from CURL
for /f %%a in ('curl -s -o nul -w "%%{time_total}" "https://www.google.com" 2^>nul') do set "latency=%%a"

echo [PERF] Response Time: %latency% seconds

:: Batch cannot compare decimals natively, so use PowerShell
:: (curl always outputs '.' as the decimal separator regardless of locale)
powershell -Command "if (%latency% -gt 2.5) { exit 1 } else { exit 0 }"
if %errorlevel% neq 0 echo [WARN] Service is experiencing high latency!

endlocal

How to Avoid Common Errors

Wrong Way: Using "Ping" to check a Web Service

A server might "Ping" (ICMP response) but the web server (IIS/Apache/Nginx) might be crashed.

Correct Way: Use curl. It tests the actual application layer. A server can be "Pingable" but still return a 502 Bad Gateway error for the website.

Problem: SSL Certificate Errors

If your internal web service uses a self-signed certificate, curl will fail by default.

Solution: Use the -k (or --insecure) flag to tell curl to ignore certificate warnings for internal monitoring. curl -k -s ...

Best Practices and Rules

1. Connection Timeouts

Always use --connect-timeout. Without it, if the server is halfway through a crash, the script might hang forever waiting for a packet that will never come.

2. User-Agent Strings

Some firewalls block the default "curl" user agent. Use -A "Mozilla/5.0" to make your monitoring script look like a standard web browser.

3. Log Rotation

If you run this monitor 24/7, your uptime.csv will grow very large. Periodically archive older logs or clear the file if it exceeds a certain size.

Conclusions

Building a web service monitor with a Batch script is a fast, lightweight alternative to complex enterprise monitoring tools. By leveraging the power of curl and simple loops, you gain real-time visibility into your infrastructure's health. This proactive approach ensures you are the first to know when a service fails, allowing you to react and resolve issues before they impact your end-users.

Navigation