The Server Health Check
Write healthcheck.sh, a script that checks a short list of things a server needs and reports OK or FAIL for each, like a real monitoring script. Use a function so each check is one line.
How it works: the terminal below is a real Linux machine running in your browser. Click it and
type. Tasks tick themselves off as you complete them; questions are answered in the box under each task.
Press Reset any time for a fresh machine. Your password on this machine (for
sudo) is student.
Hints
Write the function first and call it once; add the checks when it works.
"$@" inside the function runs whatever command and arguments you passed after the label.
Solution explanation
The check function takes a label and then a command to run. It runs the command quietly and prints OK or FAIL, setting fail=1 on any failure. id deploy and test -s /srv/app/config are ordinary commands whose exit codes say whether the check passed, and exit $fail makes the whole script succeed only if nothing failed.
Show solution
cat <<'EOF' > healthcheck.sh
#!/bin/bash
fail=0
check() {
local label=$1; shift
if "$@" >/dev/null 2>&1; then
echo "$label: OK"
else
echo "$label: FAIL"
fail=1
fi
}
check "user deploy" id deploy
check "config present" test -s /srv/app/config
exit $fail
EOF
chmod +x healthcheck.sh
./healthcheck.sh; echo $?