Linux 'watch'

Real-Time Monitoring with `watch` (feat. `grep`)

Preview

watch re-runs a command on a timer and redraws the full screen. Use it to build quick, live dashboards—counts, top-N, or snapshots. When you need streaming of only new lines, prefer tail -F | grep; when you need periodic aggregation, use watch.


Essentials


Make Colors Work with grep

watch --color -n 1 "grep --color=always -nE '(ERROR|WARN)' /var/log/app.log | tail -n 20"

Ready-to-Paste Recipes

# 1) Live error counter (snapshot)
watch -n 2 "grep -cE 'ERROR|CRITICAL' /var/log/myapp.log"

# 2) HTTP status distribution (Nginx/Apache)
watch -d -n 2 "awk '{print \$9}' /var/log/nginx/access.log \
 | grep -E '^[1-5][0-9]{2}$' | sort | uniq -c | sort -nr | head"

# 3) Top offenders (e.g., user/service keys in app logs)
watch -d -n 3 "grep -Eo 'user=[^ ]+' app.log | sort | uniq -c | sort -nr | head"

# 4) Multi-file fatal scan with filenames
watch --color -n 1 "grep --color=always -HnE '(FATAL|OOM|panic)' /var/log/*.log | tail -n 30"

# 5) “Last minute” pulse (systemd journals)
watch -n 5 "journalctl -u myservice --since '-1 min' --no-pager | grep -cE '(ERROR|WARN)'"

Quoting & Shell Gotchas


When Not to Use watch