Linux Labs

Bash: text processing and scripts / Stage 7 of 9

Exit status and a healthcheck

Not running Starting usually ~20 s
Leave the lesson

Step 7 of 8 · lab · 25 minmedium

Write a healthcheck to a contract

The deploy needs a script that tells whether an address is ready for traffic. The answer is an OK or FAIL line and an exit status, even when the server stays silent.

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.

Lab · on your own

No commands are given in this lab: type them in the terminal yourself. Goals are checked on the machine after every command.

Goals

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

The machine is starting

Hints

0 of 3

Lab goals

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

Lab goals

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