Skip to content

Check If Website Is Up

monitorcurluptime
5 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. curl comes preinstalled on macOS and nearly every Linux distribution; on a minimal image, apt install curl covers it. Schedule with cron every 5 minutes: */5 * * * * /home/user/uptimecheck.sh.

A little side project I host on a cheap VPS had been throwing errors for most of an afternoon, and I found out the way you never want to: by clicking my own link to show someone, and watching it hang. Best guess, it had been down a few hours. Nothing alerted me, because nothing was watching it — I'd stood the thing up, pointed DNS at it, and quietly assumed it would tell me if it fell over. Servers don't do that. They go dark and wait for you to notice.

The uncomfortable part is how cheap the fix was relative to all that silence. One curl that fetches the URL, reads the HTTP status code, and shouts when it isn't 200 would have caught the outage in the first five minutes instead of the third hour. The mechanism that bites here is the timeout: without --max-time, curl will sit there forever against a server that accepts the connection but never answers, and your check hangs right alongside the thing it was meant to watch. A status of 000 means it never connected at all. The script below is the watcher I should have written the day I deployed — the part I wasn't willing to leave to luck a second time.

The uptime check I should have written months earlier

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. When the check fires because a service is down (500 or 000), the restart service watchdog handles automatic recovery within 60 seconds. If your site normally redirects (e.g. HTTP → HTTPS), see the variation below to follow redirects before checking the code.

Getting the check running

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

Versions for redirects, email, and many sites

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

For full SMTP configuration, Gmail relay via msmtp, and send-once lockfiles to avoid inbox flooding, see the bash email alert script.

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"

Where uptime checks lie to you

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. To SSH into the server to investigate without a password prompt, set up the SSH key setup script first.

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.

Part of the Server Monitoring collection

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

PAID RESOURCE — $9

The Production Bash Toolkit

6 scripts + shared library + 52-page field guide. The production layer the free snippets don't cover.

Get the Toolkit →

Related Snippets

Frequently Asked Questions

faq — snippet

How do I run this script?

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

faq — snippet

Does this work on macOS?

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

faq — snippet

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.

faq — snippet

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.