Search code examples
linuxbashinotifywait

bash read: group read results by timeout


Short story: On my Linux desktop, I want for notifications whenever nodes under /dev are created or deleted (it's really useful to know what nodes are created when I plug some device in). I wrote two naive scripts for that:

The first one writes these changes to the log file by means of inotifywait:

#!/bin/sh

inotifywait -m -e create,delete --timefmt '%Y.%m.%d-%H:%M:%S' --format '[%T] %e %w%f' /dev > /var/log/devdir_changes

Resulting log file looks like this:

[2014.08.19-01:32:51] CREATE /dev/vcs63
[2014.08.19-01:32:51] CREATE /dev/vcsa63

And the second script that monitors that log file (with bash read command) and shows notifications:

#!/bin/sh

while true; do

   # -----------------------------------------------------------------------------------
   # Now, listen for new messages
   echo "listening for new messages.."

   tail -f -n 0 /var/log/devdir_changes | \
      while read time type message; do
         notify-send "$type" "$message"
      done

      echo "restarting in 5 seconds.."
      sleep 5
      echo "restarting.."
done

echo "exiting."

It works, but, as expected, there is dedicated notification balloon for each created/removed node. Usually there are several nodes when I plug single USB device, sometimes really a lot of them. So, when new entry is detected, I'd wait for some time (say, 200-300 ms) for more entries, and only after timeout after last received entry, cry collected entries with notify-send.

I'm not experienced bash programmer (and linux user), so I'd be glad if someone give me some clue on how to implement this correctly.


Solution

  • I am not too experienced with bash but I think you could feed tail's output to a while loop like in the following bash script:

    #/bin/bash
    
    # maximum time between records to be grouped
    # in seconds, e.g. "0.300" for 300ms
    #TIMEOUT=0.300
    TIMEOUT=3.1
    
    # maximum number of records to be grouped
    LIMIT=100
    
    LINE_BREAK=$'\n'
    
    # tail -f -n 0 /var/log/devdir_changes | \
    while true
    do
        declare -a times types messages
    
        # wait for, read and store first record
        read time type message
        times[0]="$time"
        types[0]="$type"
        messages[0]="$message"
        size=1
    
        # wait for more records to appear within timeout
        while [ $size -lt "$LIMIT" ]
        do
            read -t "$TIMEOUT" time type message || break
            times[$size]="$time"
            types[$size]="$type"
            messages[$size]="$message"
            size=$((${size} + 1))
        done
    
        # build message from record group
        message="${types[0]} ${messages[0]}"
        i=1
        while [ $i -lt $size ]
        do
            message="$message$LINE_BREAK${types[$i]} ${messages[$i]}"
            i=$((i + 1))
        done
    
        # send message as notification
        echo "$message"
        # notify-send "$message"
    done
    

    The key is using a timeout (-t 3.1) in the call to read and buffering input (in arrays) until either a timeout is reached or the buffer "is full" (limit 100 in example). The timeout is given in seconds, use 0.3 for 300ms.

    (Edit 1: some comments, no timeout for first record)


    Edit 2: In order to make grouping of lines by time of availability more reusable you could use a function:

    # group lines which get available at the same time
    #
    # This can be used to group lines from asynchronous input
    # according to (sufficiently large) time gaps between lines.
    #
    # $1 = max seconds to wait for another line; default: 1.5
    # $2 = max number of lines to read; default: 10
    # $3 = group terminator to use; default: $'\0'
    function time_group_lines() {
        local timeout="${1:-1.5}"
        local limit="${2:-10}"
        local terminator="${3}"
        local line
        while true ; do
            read line || return 0
            echo "$line"
            size=1
            while [ $size -lt "$limit" ] ; do
                read -t "$timeout" line || break
                echo "$line"
                size=$(($size + 1))
            done
            if [ -z "$terminator" ]
            then echo -n -e "\x00"
            else echo -n "$terminator"
            fi
        done
    }
    
    # tail -f -n 0 /var/log/devdir_changes | \
    # sed 's/^[^ ]* //' \
    time_group_lines "$TIMEOUT" "$LIMIT" | \
    while true ; do
        read -d $'\0' group || break
        # notify-send "$group"
        echo -e "$group"
    done