DEV Community

Alex Chen
Alex Chen

Posted on

Quick Tip: Tail a Log and Get a Desktop Notification When a Keyword Appears

Quick Tip

Stop watching deploy logs scroll by. Make the log watch you:

# Linux — notify when the deploy finishes (or fails)
tail -f deploy.log | grep --line-buffered -E "SUCCESS|FAILED" | \
  while read -r line; do
    notify-send "Deploy" "$line"
  done
Enter fullscreen mode Exit fullscreen mode
# macOS equivalent
tail -f deploy.log | grep --line-buffered -E "SUCCESS|FAILED" | \
  while read -r line; do
    osascript -e "display notification \"$line\" with title \"Deploy\""
  done
Enter fullscreen mode Exit fullscreen mode

The key flag is --line-buffered — without it, grep buffers output in 4KB blocks and your notification arrives 20 minutes late (or never, if the log goes quiet).

Works on any piped stream, not just files:

# Ping me when the test suite hits a failure
pytest -v | grep --line-buffered "FAILED" | \
  while read -r line; do notify-send "Test failed" "$line"; done
Enter fullscreen mode Exit fullscreen mode

I keep this in a shell function so it's one word:

watchfor() {  # usage: watchfor PATTERN
  grep --line-buffered -E "$1" | while read -r line; do
    notify-send "watchfor" "$line"
  done
}
# tail -f app.log | watchfor "ERROR|CRITICAL"
Enter fullscreen mode Exit fullscreen mode

Powered by MonkeyCode — free AI coding assistant.

What's the log line you wish had notified you before the standup meeting?

Top comments (0)