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.

6 Answers 6

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).

6
  • 5
    Don't replace the newline, add it at the end of the line.
    – Kevin
    Nov 23, 2011 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, 2011 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. Nov 23, 2011 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, 2011 at 20:38
  • 1
    @Gilles: Note that the $'...' syntax is not universal. For example, [t]csh doesn't support it. Nov 23, 2011 at 21:13
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
5
  • 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, 2011 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. Nov 23, 2011 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, 2011 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, 2011 at 2:57
  • Same applies to Tmux. Sep 15, 2017 at 9: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
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).

3
  • Of note is that $'...' escape sequences are a Bash-only thing. Nov 24, 2011 at 4:12
  • @amphetamachine thanks, while I am a bash user myself, it would be nice if you or others can add versions that would work on other shells as well.
    – Ali
    Nov 24, 2011 at 4:21
  • I'm also a bash user, but I'm slowly learning what will and won't work in other shells. Nov 24, 2011 at 6:44
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...

1
  • 2
    I can't help myself, I've to ask, can you explain sed expression? Feb 18, 2016 at 7:10
-1

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

1
  • 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, 2011 at 2:40

You must log in to answer this question.

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