Linux Labs

Bash: text processing and scripts / Stage 7 of 9 · step 7 of 8

Write a healthcheck to a contract

medium ~25 min
Not running Starting usually ~20 s
Report a problem

Situation

The deploy does not send traffic to a new app version right away: first it asks the ~/bash-lab/healthcheck.sh script whether the address is ready. The deploy looks at the exit status, the on-call engineer reads the output line. The script does not exist yet — there is a stub with a shebang line.

A test app runs on the machine for your experiments, http://127.0.0.1:8181:

Path Response
/ready 200
/unready 503
/empty 204
/redirect 302 to /ready
/slow 200, but after 10 seconds

Nothing listens on port 8182.

Task

Finish ~/bash-lab/healthcheck.sh to the contract with the deploy:

  1. The deploy runs the script by its path: ~/bash-lab/healthcheck.sh URL.
  2. The server answered 200 — the script prints the line OK to stdout and exits with 0.
  3. The server answered anything else (503, 204, 302, 404) — FAIL and exit status 1. The script does not follow a redirect.
  4. The server refuses the connection — FAIL and exit status 1.
  5. No response for 2 seconds — FAIL and exit status 1; the script ends by itself within 3 seconds.
  6. No address or two arguments — empty stdout, the line Usage: healthcheck.sh URL on stderr, exit status 2.

When the server answered — with any code — stderr stays empty: the deploy treats a line there as a failure of the check itself.

The check runs your script as student on these addresses and on its own — with other paths and responses.

The machine has no Internet; everything you need is already installed.

Visible only to you. Saved as you type.

All notes

Lab goals

0 of 6
  • ~/bash-lab/healthcheck.sh runs by its path as a command — not checked yet
  • A 200 response gives OK on stdout, exit status 0, empty stderr — not checked yet
  • Any other server response gives FAIL, exit status 1, empty stderr — not checked yet
  • A server that refuses the connection gives FAIL, exit status 1 — not checked yet
  • No response for 2 seconds gives FAIL, exit status 1, and the script ends by itself within 3 seconds — not checked yet
  • No address or two arguments give Usage on stderr, empty stdout, exit status 2 — not checked yet

Hints

0 of 3 open · they open one at a time
Hint 1
Show the solution after 3 failed checks

Leave the lab?

The machine keeps running for 10 more minutes, then it is removed. Files are lost; completed goals stay.

Loading…