Interview Prep/Shell Scripting/Best practices and rapid-fire commands

What logging practices do you follow in automation scripts?

IntermediateConceptBest practices and rapid-fire commands
Answer
  • One line per event: UTC ISO 8601 timestamp, level, script name, message (and key=value fields).
  • Logs to stderr; data to stdout.
  • Under systemd, just write to stdout/stderr; journald adds metadata. Elsewhere, logger -t myscript -p user.err "msg" sends to syslog.
  • Log start, end, duration and the decision points ("skipped: lock held", "pruned 3 backups").
  • Never log secrets; mask tokens (${token:0:4}****).
  • Rotate any file logs with logrotate.

The goal: at 3 AM, journalctl -u backup --since today tells you what happened without reading the code.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Best practices and rapid-fire commands