Monitoring with sound

Malarose03

New Member
Joined
Aug 23, 2020
Messages
4
Reaction score
0
Credits
43
Hi,

How can i get a sound while monitoring a file using tail with grep.

E.g.
Tail -f log.file | grep 'Network'

If there any display with the following command there the system should produce a sound/soundtrack so that we need not to keep an eye always on the log file and the sound will alert us that there is a match found.

Also, let me know if there are any prerequisites like availability of a Sound File or any Sound Package Installed, if so let me know what details are prerequisites and also where can i check if the things are already available .

Thanks in advance.
 
Last edited:


Hi,

How can i get a sound while monitoring a file using tail with grep.

E.g.
Tail -f log.file | grep 'Network'

If there any display with the following command there the system should produce a sound/soundtrack so that we need not to keep an eye always on the log file and the sound will alert us that there is a match found.


Thanks in advance.
Also, let me know if there are any prerequisites like availability of a Sound File or any Sound Package Installed, if so let me know what details are prerequisites and also where can i check if the things are already available .

Thanks in advanve.
 
Offhand, I can't think of a built-in way of getting a sound notification using tail -f.

But you could write a script to monitor the log-file and then make a sound when a new error is detected.

Just off the top of my head, this is a bit crude. It might not be perfect, but will probably be along the right lines.
Bash:
#!/usr/bin/env bash

# The log-file to monitor
INFILE="log.file"

# Show existing lines in the log that contain 'Network'
grep 'Network' "$INFILE"

# Check the last line in the file for "Network"
PREVIOUS="$(tail -n 1 "$INFILE" |  grep 'Network')"
while true; do
    # Check the last line in the file for "Network"
    OUTPUT="$(tail -n 1 "$INFILE" |  grep 'Network')"
    # if current output is empty - then something was written to the file,
    # but was not network related
    if [[ -z $OUTPUT ]] ; then
        PREVIOUS="";
    elif [ "$OUTPUT" != "$PREVIOUS" ] ; then
        # A new Network related error was detected
        espeak "Alarm!" 2&> /dev/null &
        echo "$OUTPUT"
        # Set previous to the current message
        PREVIOUS="$OUTPUT"
    fi
done

That will display any lines containing "Network".
It will then monitor the last line of the file and any time a new line containing "Network" is detected - it will sound an alarm (via espeak) and will display the newly detected error message.

The above script uses espeak. which you may or may not have installed. If you don't want to use espeak, you could use any number of things to play a sound. So you could replace the call to espeak with a call to aplay (alsa player), or paplay (pulse audio player), or cvlc, or mplayer etc etc. Whatever you have installed!
e.g.
To use aplay to play a sound - replace the espeak line in the script with:
Bash:
aplay /path/to/sound.file 2&> /dev/null &
Where /path/to/sound.file is the path to the sound-file to play when a new "Network" related error has been detected.

NOTE: the 2&> /dev/null part of that line suppresses any textual error messages from aplay, sending them to /dev/null instead of onscreen. That way the script only shows lines of text from the log that contain the word "Network".

And the & at the end of the line - runs aplay in the background, as a separate process. That way the script doesn't have to wait for aplay to finish playing the sound and can re-iterate through the loop to continue checking the log-file.


ALSO, this script has a few limitations.
1. It will only work properly if each line written to the log has some kind of unique information, like a time-stamp.
If each line only contains generic error messages, with no time-stamp, or no other unique data - then if a duplicate error message is added to the log-file, this script will miss it because the new error message will appear to be the same as the previous one.

2. This script is only checking the very last line of the log. So if the log-file gets more than one line written out at once - we could potentially end up missing some "Network" events.

If either of these cases are problematic, then you might have to modify the above script to do something slightly different.

Like, perhaps get an initial line-count for the log-file and it's modification time-stamp at the start and then monitor the log-files time-stamp.
If the file has been modified - get an updated line-count, determine how many lines have been added and then check those lines for "Network" before re-iterating through the loop.

Hopefully this helps a little?!
 
Hi, wonderful ! ❤
I was not searching this BUT it shows me ONC MORE in LINUX and with knowledge - EVERYTHING IS POSSIBLE !
thanks to the one which has made the ' code lines ' and posted them here , Merci from Sofia / Bulgaria where I stay now in the center, very near to the demonstration camp, they want that the prime minister is stopping his work !
 


Top