Skip to content

Instantly share code, notes, and snippets.

@torbiak
Created October 9, 2015 19:05
Show Gist options
  • Save torbiak/044cf164ab8a6ff80a5f to your computer and use it in GitHub Desktop.
Save torbiak/044cf164ab8a6ff80a5f to your computer and use it in GitHub Desktop.
Periodically monitor a command's output for changes.
#!/usr/bin/env bash
# Monitor a command's output for changes.
set -eu
usage="diffmon [-D] [-t INTERVAL] CMD ARGS...
-t INTERVAL Interval to sleep between command executions
-b Print a blank line between changes instead of the date"
interval=1
show_date=yes
while getopts "hbt:" o; do
case "$o" in
t) interval=$OPTARG;;
b) show_date='';;
h) echo "$usage"; exit;;
*) echo "$usage" 1>&2; exit;;
esac
done
shift $((OPTIND - 1))
if [[ $# -lt 1 ]]; then
echo "$usage" 1>&2
exit
fi
old=/tmp/diffmon.old.$(date '+%s')
new=/tmp/diffmon.new.$(date '+%s')
trap "rm $old $new" TERM
"$@" > $old
while true; do
"$@" > $new || true
diff -N -u0 $old $new | sed -re '/^(---|\+\+\+|@@)/d'
if [[ ${PIPESTATUS[0]} -eq 1 ]]; then
if [[ -n $show_date ]]; then
echo -n '@'; date -Iseconds
else
echo
fi
fi
mv $new $old
sleep $interval
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment