24

Is there a way to make tail -F or less beep (ring the bell in a terminal) when new data comes in (a new line is added to the file). Or is there any other unix utility to do this on a linux or mac.

Ali
  • 6,943

6 Answers6

14

An idea migth be to pipe the output of tail through sed and replace the newline with bell/newline.

But there is propably an easier solution if you use tail within an x-window. There your can execute an action when the content of the window changes (flicker, bell, whatever).

Nils
  • 18,492
  • 5
    Don't replace the newline, add it at the end of the line. – Kevin Nov 23 '11 at 19:42
  • @Nils, this sed Idea is brilliant but I don't know enough 'sed' to make it work, it looks like I cant capture newlines in the regexpt the usual way (that I am used to), I guess I need to tell sed dont interpret the incoming stream line by line. – Ali Nov 23 '11 at 20:13
  • 5
    @Ali Indeed, sed acts line by line and you don't directly see the newlines. sed -e $'s/$/\a/' adds a $'\a' (bell character in ksh/bash/zsh syntax) at the end of each line. – Gilles 'SO- stop being evil' Nov 23 '11 at 20:35
  • Thanks @Gilles , worked like a charm (on a mac too)! sed regexpt is a bit odd to my naive eyes. – Ali Nov 23 '11 at 20:38
  • 1
    @Gilles: Note that the $'...' syntax is not universal. For example, [t]csh doesn't support it. – Keith Thompson Nov 23 '11 at 21:13
  • @KeithThompson: That's not the only kind of quoting that [t]csh doesn't support! – Cascabel Nov 23 '11 at 23:10
13

if you use GNU screen, you can set it to "watch" the window with the tail, and it will alert you in your status bar, or by your termcap's defined bell, that there is new output in that window.

http://www.gnu.org/software/screen/manual/html%5Fnode/Monitor.html#Monitor


edit: just had to add this, since you mentioned mac os x

just for fun, if you were looking for something in particular, you can use Mac OS X's say command to read you the file you're watching. just get the logtail command, from:

http://www.hmug.org/pub/MacOS_X/BSD/Administration/Log/logcheck/

And use it in a script like:

#!/bin/bash

file=$1
offset=$(basename "$1")

# while true... let this thing run until it's killed...
while true; do
    output=$(/usr/local/bin/logtail $file .${offset}.offset)
    if [ ! -z "$output" ]; then

        # print the output and say ding
        echo "$output" && say ding
        # to have the file read aloud to you, uncomment the following:
        say "$output"

        fi
    # recheck every 5 seconds
    sleep 5
done
Tim Kennedy
  • 19,697
  • Thanks @Tim, screen , shows and alert (I guess I can make it ring the bell as well) ONLY ONCE. Not for every new event that happens (not for every new line that is available) – Ali Nov 23 '11 at 20:11
  • 1
    i added an example for mac that could read the file to you. you could change that to work for linux by installing the linux version of logtail, and using the beep command, instead of say. – Tim Kennedy Nov 23 '11 at 20:29
  • cool! That is creative, although in my case I am using the beep to be able to monitor a log file without looking at screen. And the actual beep may be better than a voice. – Ali Nov 23 '11 at 20:40
  • 1
    Can also use screen's exec command to do it as well. The example in the manpage is !:sed -n s/.*Error.*/\007/p which will send a bell each time "Error" is displayed in that window. – Arcege Nov 24 '11 at 02:57
  • Same applies to Tmux. – 0xC0000022L Sep 15 '17 at 09:51
10

You could use multitail. It is an enhanced tail that supports command execution on regular expression match.

E.g. the following command plays a sound and opens an xmessage window each time a martian source packet is logged.

multitail -ex "martian source" "play beep.wav; xmessage " -i /var/log/messages
andcoz
  • 17,130
6

Just for the record, as @Nils suggested I am using sed to add a bell to each line.

The sed line provided by @Gilles

sed -e $'s/$/\a/' 

works on my mac (I enabled "audible bell" and "visual bell" in my Terminal\preferences\advanced).

Ali
  • 6,943
1

For anyone else who finds this page, I'm using tcsh and this seems to work:

tail -f changingfile | sed -e 's/.*/& \a/'

Don't ask me what the sed syntax means...

slm
  • 369,824
raymond
  • 11
-1

You can try konsole which allows alert on new activity (or silence) in any given tab.

  • 1
    I imagine konsole would be like screen it alerts once, while I need to receive an alert for every new line. – Ali Nov 24 '11 at 02:40