Chapter 4 · Lesson 6 of 18
Deploy and operate

Monitor uptime simply

Uptime monitoring means checking on a schedule that your app answers correctly, and alerting you when it does not.

By the end you will

Set up a health check with curl and cron that alerts you when your app stops answering.

Time
18 min
Level
Beginner
Check
4 questions
You need
  • A deployed app with a public URL
  • A way to send an alert, such as email or a chat webhook

You want to know about an outage before your users tell you. A deploy that quietly broke the login page at 2 a.m. is found by a check, not by luck. A small script is enough to start.

How it works#

An uptime check is a request sent on a schedule. It passes when it gets the expected answer inside a time limit, and fails otherwise. Three details decide whether the check is any good.

What it asks. A check against the home page proves the web server works. A check against a /health path that also runs a trivial database query proves more: the app can do its job. Keep the path fast and free of private data.

How it judges. curl exits with code 0 for any HTTP response unless you ask otherwise, even a 500. The -f flag turns 4xx and 5xx into a non-zero exit code, and --max-time turns a hang into a failure.

Where it runs. A check on the server you monitor cannot report that the server is down, because it is down too. Run quick custom checks from cron, and also use a checker on another network.

Step 1: Add a health endpoint#

Expose a path such as /health that returns HTTP 200 when the app works. If it depends on a database, make it run a trivial query so a broken connection shows up as an error status.

What you should see: opening /health in a browser shows a short body such as ok, with status 200.

Step 2: Check it with curl#

bash
curl -fsS --max-time 10 https://example.com/health

The flags matter:

  • -f makes curl exit with an error on HTTP 4xx or 5xx.
  • -s hides the progress bar and -S still prints errors, so together they keep output quiet but useful.
  • --max-time 10 gives up after 10 seconds so a hung server counts as a failure.

What you should see: the response body, and echo $? right after prints 0. On a failing app curl prints a line such as curl: (22) The requested URL returned error: 503 and the exit code is 22.

Step 3: Run it on a schedule#

Create a small script check.sh:

bash
#!/bin/sh
curl -fsS --max-time 10 https://example.com/health > /dev/null \
  || echo "health check failed" | mail -s "App down" you@example.com

Make it executable with chmod +x check.sh, then add it to cron with crontab -e:

bash
* * * * * /home/you/check.sh

The five stars mean every minute. Use the full path to the script. The mail command needs a mail setup on the host, so you can swap it for any alert you already use, such as a chat webhook with curl -X POST.

What you should see: crontab -l lists the line. A failing check produces one alert per minute until it recovers, which is why you may later want to alert only on the second failure.

Step 4: Add an outside checker#

Also use an external uptime checker that requests your URL from another network and notifies you by email or chat. Use both: cron for quick custom checks, the outside one for the user’s view.

What you should see: the checker’s dashboard shows your URL as up, with a response time.

Check it worked#

Run the curl line by hand: it prints the response body, and echo $? right after prints 0. Then break the check on purpose, for example by pointing it at a wrong path such as /does-not-exist, and confirm the exit code is non-zero and you receive the alert. Put the real path back afterwards.

Edge cases and gotchas#

  • Cron has a bare environment. Its PATH is short, so mail or curl may not be found. Use full paths such as /usr/bin/curl in the script.
  • A redirect can hide a failure. If http:// redirects to https://, a check without -L sees a 301 and curl exits 0. Point the check straight at the final HTTPS URL.
  • A 200 from a cache or CDN can mask a dead origin. If a CDN serves a cached page, your check passes while the app is down. Make the health path uncacheable, or check the origin directly.
  • IPv6 and IPv4 can differ. If your domain has an AAAA record and the server only listens on IPv4, some checkers fail while others pass. Test with curl -4 and curl -6.
  • Checks can be rate limited or blocked. A firewall, WAF or fail2ban rule can ban your checker’s IP after many requests. Allow it, or lower the frequency.
  • An expired certificate fails the check. curl refuses the connection with a certificate error. That is useful: it warns you before users see it, but do not “fix” it with -k, which turns off verification.
  • Clock drift and overlapping runs. A check that takes longer than the interval can overlap itself. Keep --max-time below the interval.

Troubleshooting#

SymptomLikely causeFix
Alert never arrivesThe mail command or its setup does not workRun the alert command by itself and read its output
Script works by hand but not in cronCron’s minimal environment and PATHUse full command paths and absolute file paths
curl: (22) The requested URL returned error: 502The app or proxy upstream is downSee Fix a 502 Bad Gateway
curl: (28) with a timeout messageThe server is slow or hung, or a firewall drops packetsCheck load and logs; raise --max-time only if slowness is normal
curl: (6) Could not resolve hostDNS problem or a typo in the URLCheck the name with dig example.com
curl: (60) certificate errorExpired or mismatched certificateRenew the certificate, see the renewal lesson
False alarms every few daysOne slow response triggers an alertAlert only after two failures in a row
Alert says down, but the site loads for youChecker is blocked, or only one region or IP family failsTest from another network and with curl -4 and curl -6
Check passes but users see errorsHealth path is too shallowMake /health run a database query and return 503 when it fails

Real-world scenario#

You run a small booking app on one server. At 02:10 a certificate renewal fails quietly. At 02:12 your external checker reports an invalid certificate and sends a chat message. Meanwhile the cron check on the server still passes, because it calls http://127.0.0.1:3000/health directly and never touches HTTPS.

You open the chat message, run the public check yourself and read the certificate error:

bash
curl -fsS --max-time 10 https://example.com/health

curl prints curl: (60) SSL certificate problem: certificate has expired. You renew the certificate, run the same command again and get ok. The lesson: the outside checker caught what the local one could not, because it tests the same path your users take.

Quick reference#

GoalCommand or setting
One-off checkcurl -fsS --max-time 10 https://example.com/health
Show only the status codecurl -s -o /dev/null -w '%{http_code}\n' https://example.com/health
Run every minute* * * * * /home/you/check.sh
Exit code after curlecho $? (0 is success)
Avoid noiseAlert after two failures in a row

Next#

When a check fails, look at the logs: Read container logs.

Check what you learned4 questions
1What does curl -f change?
2Why add an outside uptime checker as well as a cron check?
3Your checks give false alarms from brief slowness. What is a sensible fix?
4Your cron check never alerts, even when the app is down. What is a likely cause?

Frequently asked questions

Why check from outside my server?

If the server or its network is down, a check running on that same server cannot report it. An outside checker sees what your users see.

What should a health endpoint return?

A 200 status when the app can do its job, and an error status when it cannot. Keep it fast and do not expose private details.

Why does my check say OK while users see an error page?

Your check may hit a different path or skip the proxy. Check the public URL users use, and make the health path test what matters, such as the database.

How often should I check?

Every minute is a common start. More often adds load and noise without much gain. Alert after two failures in a row to avoid false alarms.

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios