Search code examples
bashshelltailgpioodroid

tail a constantly updated logfile and perform an action when a string is found


I have an odroid-c1+ that I would like to use as a pi-hole server (basically dns blackhole for ad's)

I would like to trigger an led to blink when a string is found in the logfile.

I also have wiringpi installed and working, the example blink.sh works as expected as follows:

PIN=0

gpio mode $PIN out

while true; do
  gpio write $PIN 1
  sleep 0.5
  gpio write $PIN 0
  sleep 0.5
done

How would one go about adding the tailf trigger to this sample?


Solution

  • Untested, but I believe you can feed the output from tail into your while loop:

    #!/bin/bash
    pin=0
    gpio mode $pin out
    tail -f logfile | while read entry
    do
       if [ "$entry" = "string" ]; then
           gpio write $pin 1
           sleep 0.5
           gpio write $pin 0
           sleep 0.5
        fi
    done
    

    Uppercase variable names are traditionally reserved for the shell's use.