Write a script that monitors a log file in real time and sends a Slack message on "ERROR".

IntermediateHands-onBash scripting tasks
Answer
#!/usr/bin/env bash
set -euo pipefail
WEBHOOK="${SLACK_WEBHOOK:?}"
tail -Fn0 /var/log/app/app.log | grep --line-buffered 'ERROR' | while read -r line; do
  payload=$(jq -n --arg t "$(hostname): $line" '{text: $t}')
  curl -s -X POST -H 'Content-Type: application/json' -d "$payload" "$WEBHOOK" >/dev/null
done

Key trick → tail -F follows the file across rotation (by name, not inode), and --line-buffered stops grep from holding output in a 4 KB buffer. jq -n --arg escapes quotes in the log line safely.

All Real-World Interview questions Roadmap: Interview readiness

More on Bash scripting tasks