tail -f, insert line break after log is idle for 3 seconds?
You could always implement the tail -f
(well here, unless you uncomment the seek()
, more like tail -n +1 -f
as we're dumping the whole file) by hand with perl
for instance:
perl -e '
$| = 1;
# seek STDIN, 0, 2; # uncomment if you want to skip the text that is
# already there. Or if using the ksh93 shell, add
# a <((EOF)) after < your-file
while (1) {
if ($_ = <STDIN>) {
print; $t = 0
} else {
print "\n" if $t == 3;
# and a line of "-"s after 10 seconds:
print "-" x 72 . "\n" if $t == 10;
sleep 1;
$t++;
}
}' < your-file
Or let tail -f
do the tailing and use perl
to insert the newlines if there's no input for 3 seconds:
tail -f file | perl -pe 'BEGIN{$SIG{ALRM} = sub {print "\n"}} alarm 3'
Those assume that the output itself is not slowed down (like when the output goes to a pipe that is not actively read).
bash
+ date
solution:
while IFS= read -r line; do
prev=$t # get previous timestamp value
t=$(date +%s) # get current timestamp value
[[ ! -z "$prev" ]] && [[ "$((t-prev))" -ge 3 ]] && echo ""
echo "$line" # print current line
done < <(tail -f error.log)
Python
solution (with dynamic time gap argument):
tailing_by_time.py
script:
import time, sys
t_gap = int(sys.argv[1]) # time gap argument
ts = 0
while True:
line = sys.stdin.readline().strip() # get/read current line from stdin
curr_ts = time.time() # get current timestamp
if ts and curr_ts - ts >= t_gap:
print("") # print empty line/newline
ts = curr_ts
if line:
print(line) # print current line if it's not empty
Usage:
tail -f error.log | python tailing_by_time.py 3