0

I always run tail -F to keep an eye on a log in a terminal window. The log has a UTC date at the beginning of each entry:

[08-Jan-2013 16:24:22] Yo!

When I glance at my log window I need to know how old the entries are. I would prefer not to have to look at my local clock and do the math. So I figured it would be cool to have a real-time clock display in the window along with the tail ouput.

What I came up with was to background a while loop echoing the date and a carriage return. When a log entry is written, tail overwrites the clock.

while true; do echo -ne `date +"[%d-%b-%Y %H:%M:%S]"`"\r"; sleep 1; done &
log -F /path/to/log

This basically works but I'm concerned about what happens when outputs from tail and date produce output at the same time. I don't want my view of the log to be spoiled by the carriage return.

How would I multiplex these outputs together so that they can't collide? Is that a job for screen or something else?

The log is on a remote Debian server. My workstation runs OSX. I'm connecting via SSH to run the script above.

3
  • This seems more like a programming problem than a system administration problem and as such may be better served on Stack Overflow.
    – user9517
    Jan 8, 2013 at 17:19
  • You could use tail -f and then pipe the output to sed to change the text to something else. But first of all changing the time to current time would be difficult, and then, as the text is already on the screen you would need the time to change while displayed on the screen. I would go to something more flexible, like php binary. Jan 8, 2013 at 18:02
  • I've done something similar in the past where I had the following running in the background: while sleep 1; do echo done & While it doesn't show time since last log, it does make older logs scroll out of sight (which may or may not be desirable). Aug 2, 2016 at 10:52

2 Answers 2

3

You could use the -s option of tail so that your tail sleeps for say 5 seconds:

-s, --sleep-interval=S
 with -f, sleep for approximately S seconds (default 1.0) between iterations.

tail -s 5 -f /var/log/messages

Or the watch command will show you the date:

watch --interval=5 tail  /var/log/messages

    Every 5.0s: tail /var/log/messages                 Tue Jan  8 17:52:41 2013

Jan 8 17:52:34 localhost kernel: NEW: IN=eth0 OUT= MAC=00:60:56:73:14:6b:00:50:5b:93:49:5d:08:00 SRC=10.0.0.2 DST=10.0.0.1 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=29070 DF PROTO=TCP SPT=50561 DPT=25 WINDOW=5840 RES=0x00 SYN URGP=0

Another (not particularly nice) solution using strace -t command:

 strace -t tail -f /var/log/messages

Finally, you could write your own modified tail command in python/perl/ruby:

https://stackoverflow.com/questions/136168/get-last-n-lines-of-a-file-with-python-similar-to-tail

and just add date/time read to the output line.

2
  • See this similar question:serverfault.com/questions/157526/…
    – gm3dmo
    Jan 8, 2013 at 18:30
  • Thanks for these. I also just found and tried tmux which I didn't like as much. As it turns out, the code that I posted has been running all day with no conflicts.
    – Andy
    Jan 8, 2013 at 22:07
3

You may want to have a look at multitail; it supports multiple windows with separate or merged log files, regex-based coloring per file type, word-wrap options, filename watch (useful for logrotated files), and more.

Oh, and a clock on the status line.

1
  • Very nice program! I will find it useful. Still, the clock on the status line only updates when there is output so multitail doesn't meet the goal. I want to know how old the last log entry is without consulting a clock outside the terminal window. If multitail updated the clock or (even cooler) showed the H:M:S since the last entry it would win.
    – Andy
    Jan 15, 2013 at 15:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .