Skip to content

Instantly share code, notes, and snippets.

@vi
Created June 30, 2014 09:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vi/340a2dba88ff14abfa4f to your computer and use it in GitHub Desktop.
Save vi/340a2dba88ff14abfa4f to your computer and use it in GitHub Desktop.
#!/bin/bash
if [[ -z "$1" || "$1" == "--help" ]]; then
echo "Ouput content of the file being appended to to stdout, continue outputting as more data appears in the file, exit when no processes write to the file for a while."
echo "Usage: cat_and_follow file | something"
exit 1
fi
F="$1"
THISPID=$$
(
lsof -Fa -r 2 "$F" | perl -ne 'INIT{$wd=0;} $wd=0 if /^aw/; if(/^m/) { exit 0 if $wd > 2; ++$wd; }'
kill $THISPID;
)&
exec tail -n +1 -f "$F"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment