donderdag 28 februari 2013

watching a logfile and doing stuff

I needed to watcha logfile and trigger an action if a specific line appeared in the logs


    #!/bin/sh
    #tail the file you want to watch
    tail -fn0 /var/log/logfile | while read line ; do
            echo "$line" | grep 'line to react to' | grep -v 'things to block the reaction' 
            if [ $? = 0 ]
            then
                    # do things
                    echo "just saw $line"
                    mpg123 triomph.mp3
            fi
    done


save this as script.sh
make it exectuable (chmod +x script.sh) 
and let it run in the background ./script.sh &