Goal
Write a safe, reusable script that checks a service, collects logs, and prints a clean summary.
Script building blocks
- variables
- functions
- exit codes
- strict mode (optional)
Example: service health script
#!/usr/bin/env bash
set -euo pipefail
SERVICE=${1:-nginx}
echo "Checking: $SERVICE"
systemctl is-active --quiet "$SERVICE" && echo "ACTIVE" || echo "NOT ACTIVE"
echo "Recent logs:"
journalctl -u "$SERVICE" -n 30 --no-pager || true
Make it executable
chmod +x health.sh
./health.sh nginx
Exercises
- Add a flag
--sinceto change the log window. - Add a check for open port (using
ssornc). - Make it print a final PASS/FAIL summary.
Next Step
This is the foundation for automation in CI/CD and production operations.