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 1 of 8 · on a machine · 4 min

The check says OK

What you will learn

  • Read a command's exit status with $? right after it.
  • Build a script's conditions and result on a command's status — if, &&, || and exit.
  • Tell a curl failure from an error answer, and stop a script on a failure with set -euo pipefail.

Before handing over the shift, the on-call admin runs ~/health-lab/healthcheck.sh: the script asks the app on port 8080 and prints OK or FAIL. Today it printed OK, while users write that the site does not open. Look at the script, run it, and ask the app yourself.

Exercise · look

Run the shift check

In ~/health-lab, look at the healthcheck.sh script, run it, then ask the app yourself with curl.

First, predict

What will ./healthcheck.sh print on its last line?

Now start the exercise, run the command and compare your prediction with the output.

Sign in and start The machine is available after signing in
Check yourself 1 question

The app does not answer, yet healthcheck.sh printed OK. What did the if [ $? -eq 0 ] line check?

  • curl could not connect. Had the check been on curl, the script would have printed FAIL.

  • The script never sees the answer: -o /dev/null throws away whatever curl received.

The condition got the result of echo, which ran without an error.

Ubuntu 24.04
Loading…