Check If Website Is Up

monitorcurluptime
3 min read

Quick Answer

The curl command with -o /dev/null -s -w "%{http_code}" fetches a URL silently and returns only the HTTP status code. This script stores that code in STATUS and compares it to 200 — the only code that means the server responded successfully. A site can be technically reachable but returning 500 (server error) or 404 (not found), both of which mean users cannot access your content. Without monitoring, you find out your site is down when a user tells you — often hours or days after the outage started. The --max-time 10 flag prevents the script from hanging indefinitely when a server is completely unreachable. Works on Ubuntu 22.04 LTS, Debian 12, Fedora 39, and macOS Ventura — curl is pre-installed on all of them. Schedule with cron every 5 minutes: */5 * * * * /home/user/uptimecheck.sh.

The Script

Paste this into uptimecheck.sh. Change URL to your site. Schedule with cron and never find out your site was down from a user again.

bash
#!/bin/bash # Script: uptimecheck.sh # Purpose: Alert when a site stops returning HTTP 200 — silent outages cost real traffic # Usage: ./uptimecheck.sh set -euo pipefail CHECK="✓" CROSS="✗" URL="https://bashsnippets.xyz" STATUS=$(curl -o /dev/null -s -w "%{http_code}" --max-time 10 "$URL") if [ "$STATUS" -eq 200 ]; then echo "$CHECK $URL is up (HTTP $STATUS)" else echo "$CROSS WARNING: $URL returned HTTP $STATUS" fi

What this does, line by line

curl -o /dev/null throws away the page content — we don't need it. -s runs silently with no progress bar. -w "%\{http_code\}" prints just the status code. --max-time 10 gives up after 10 seconds so the script doesn't hang forever if the server is unreachable. The if block checks whether the code is 200 and prints accordingly.

HTTP Status Codes — What They Mean

CodeMeaningScript behaviour
200OK — site is upPrints check message, exits 0
301 / 302Redirect — common with HTTP → HTTPSFires the warning; add -L to follow redirects
404Page not foundFires the warning
500Server errorFires the warning
000No response / timeoutFires the warning — server completely unreachable

Your script will trigger the warning for anything that is not 200. A 301 redirect, 404, 500, or complete timeout — all will fire the alert. If your site normally redirects (e.g. HTTP → HTTPS), see the variation below to follow redirects before checking the code.

Step-by-Step Setup

Step 1 — Create the file

bash
nano uptimecheck.sh

Paste the script, then Ctrl+X → Y → Enter to save.

Step 2 — Set your URL

VariableExampleNotes
URLhttps://bashsnippets.xyzAlways include https:// — don't just use the domain
URLhttps://myapp.com/api/healthYou can point it at a specific endpoint, not just the homepage

Step 3 — Make executable and test

bash
chmod +x uptimecheck.sh ./uptimecheck.sh

You should see: ✓ https://bashsnippets.xyz is up (HTTP 200)

Step 4 — Schedule with cron (every 5 minutes)

bash
crontab -e
bash
# Check every 5 minutes */5 * * * * /home/user/uptimecheck.sh # Check every minute (high-stakes sites) * * * * * /home/user/uptimecheck.sh # Check every hour (low traffic sites) 0 * * * * /home/user/uptimecheck.sh

Variations

Send an email alert when the site goes down

bash
#!/bin/bash URL="https://bashsnippets.xyz" EMAIL="you@example.com" STATUS=$(curl -o /dev/null -s -w "%{http_code}" --max-time 10 "$URL") if [ "$STATUS" -ne 200 ]; then echo "$URL returned HTTP $STATUS at $(date)" \ | mail -s "⚠ Site Down: $URL" "$EMAIL" fi

Handle HTTP → HTTPS redirects correctly

If your site redirects HTTP to HTTPS, curl will see a 301 instead of 200. Add -L to follow the redirect and check the final destination:

bash
STATUS=$(curl -L -o /dev/null -s -w "%{http_code}" --max-time 15 "$URL")

Monitor multiple sites at once

bash
#!/bin/bash SITES=("https://bashsnippets.xyz" "https://myapp.com" "https://api.myapp.com") for url in "${SITES[@]}"; do STATUS=$(curl -o /dev/null -s -w "%{http_code}" --max-time 10 "$url") if [ "$STATUS" -eq 200 ]; then echo "✓ $url — UP ($STATUS)" else echo "⚠ $url — DOWN ($STATUS)" fi done

Log every check to a file

bash
#!/bin/bash URL="https://bashsnippets.xyz" LOG="/var/log/uptime.log" STATUS=$(curl -o /dev/null -s -w "%{http_code}" --max-time 10 "$URL") TIMESTAMP=$(date "+%Y-%m-%d %H:%M") echo "[$TIMESTAMP] $URL — HTTP $STATUS" >> "$LOG"

Common Mistakes

Missing --max-time causes scripts to hang

Without --max-time, curl will wait forever if the server is completely unresponsive. Your cron job will pile up with stuck processes. Always include --max-time 10 or a similar timeout.

Checking HTTP instead of HTTPS returns 301

If you check http://yoursite.com but your site redirects to HTTPS, curl will return 301 (redirect) not 200. Either add -L to follow redirects, or point your URL variable directly at https://yoursite.com.

Email alerts need mailutils installed

The email variation uses the mail command. Install it first: sudo apt install mailutils on Ubuntu/Debian. Test it with: echo "test" | mail -s "test" you@example.com before relying on it for alerts.

Frequently Asked Questions

How do I check if a website is up using bash?

Use curl -o /dev/null -s -w "%{http_code}" https://yoursite.com. This returns just the HTTP status code. 200 means up, anything else means a problem. Wrap it in an if statement and schedule with cron for continuous monitoring.

How do I get notified when my website goes down?

Add email alerts using the mail command in the else branch of your status check. Schedule the script with cron every 5 minutes using */5 * * * * ~/uptimecheck.sh. You'll receive an email within 5 minutes of any outage.

What does HTTP 200 mean?

HTTP 200 means OK — the server responded successfully. 301 is a redirect, 404 is not found, 500 is a server error, and a status of 000 means curl couldn't connect at all (timeout or DNS failure).

How do I monitor multiple websites with one bash script?

Put your URLs in a bash array and loop through them. See the "Monitor multiple sites" variation above — each URL gets checked independently and you get a separate status line for each one.

BashSnippets logo

Written by Anguishe

Creator of BashSnippets.xyz

bashsnippets.xyz/about

Run this script on a real Linux server

Get $200 free credit — DigitalOcean

Get $200 Free →

Affiliate link · we earn a commission

Need a domain for your next project?

Register with Namecheap — free WHOIS privacy included

Check Domain Prices →

Affiliate link · we earn a commission

Related Snippets

Frequently Asked Questions

How do I run this script?

Save as uptimecheck.sh, set your URL, run chmod +x uptimecheck.sh, then execute ./uptimecheck.sh.

Does this work on macOS?

Yes. curl is pre-installed on macOS. The --max-time flag works identically on BSD curl.

How do I check if a website is up using bash?

Use curl -o /dev/null -s -w "%{http_code}" URL and compare the result to 200. Non-200 means the site is down or returning an error.

How do I get notified when my website goes down?

Schedule this script with cron every 5 minutes and pipe failures to mail or a Slack webhook.